MonkBen
MonkBen

Reputation: 644

How to Determine Java Heap Size On Application Startup

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?

Upvotes: 2

Views: 488

Answers (2)

Sanju Thomas
Sanju Thomas

Reputation: 241

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.

Upvotes: 0

chiastic-security
chiastic-security

Reputation: 20520

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.

Upvotes: 3

Related Questions