2

I have a java desktop application that supports viewing very large amounts of data at a time. In order to support this, I start the application with high -Xms -Xmx settings. For example,

-Xms512m -Xmx1024m

The problem I run into is that depending on the client machine and current usage, the Java virtual machine can't always start up with such high settings. The solution is to lower the size.

Has anyone else encountered this problem? How did you solve it? Is there a way to predetermine good -Xms and -Xmx sizes? Or is there a way to specify size within the application and not at start up?

6
  • The only way to specify the size at runtime is to have it start another JVM with the desired maximum heap size. Commented Sep 17, 2014 at 20:13
  • Your application either does or doesn't need that much memory. If it doesn't, then lower the limits; if it does, then buy more RAM to be able to run it. Commented Sep 17, 2014 at 20:13
  • 1
    @MarkoTopolnik that's not really true. Sometimes an algorithm can run more efficiently with more RAM, but still get the work done with a small memory footprint. Think sorting: you can do it very efficiently if you can do it all in memory, but you can fall back to using disk space if necessary. Commented Sep 17, 2014 at 20:19
  • @chiastic-security Yes, I know that, but I have a feeling that OP's application is not designed that way. Commented Sep 17, 2014 at 20:23
  • can you give more information about the data you are loading and how you are actually loading it? It may help give another approach to achieve your task with less memory usage. Commented Sep 17, 2014 at 20:24

2 Answers 2

3

You can't determine it in your Java code, because it's too late by then: it's a property of the JVM, which has already been fired up.

You'd need to do something OS-specific to determine the available memory and start the JVM with appropriate parameters. For instance, on Linux, you could start your application with a bash script, which would start by examining /proc/meminfo to determine system config, and then fire up the Java program, setting the heap size accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer makes sense and would work. It is unfortunate there is no way to change this while running.
0

You can write a shell script or batch script to determine the server RAM capacity and based on that you can pass in the heap size parameter during the JVM start up.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.