Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set maximal jvm-memory (XMX) for a jar-file

Tags:

java

jvm

jar

How do I set the maximal jvm-memory without adding an extra batch-script to my Programm. Answer is provided below.

like image 287
Fabian Zeindl Avatar asked May 17 '10 10:05

Fabian Zeindl


People also ask

How do I allow JVM to use more memory?

Under the Java tab, select JVM Options. Edit the -Xmx256m option. This option sets the JVM heap size. Set the -Xmx256m option to a higher value, such as Xmx1024m.

How do you set the maximum and minimum value of JVM memory?

Set the memory available to the JVMTag(s): Environment-Xmx<size> the maximum Java heap size. The default value for the minimum is 2Mb, for the maximum it's 64Mb.

Which JVM parameters is used to set the maximum heap size?

You can control the heap size with the following JVM parameters: -Xms value. -Xmx value. -XX:MinHeapFreeRatio= minimum.


1 Answers

It's a good question, but your implmenetation assumes a lot. I presume you have to document the name of your jar for your users to invoke "java -jar xyz.jar" on so can you also include in the documentation that "-Xmx256M" is required?

You may have more luck using a java launcher, such as this one for Windows, where you put the launcher config (path, max memory etc.) in a separate file. For cross platform, there's LaunchAnywhere, and others, which function similarly. See How can I convert my Java program to an .exe file?

To improve upon your existing scheme:

  • use the java.home system property to resolve the location of the JDK/JRE
  • use the java.class.path to set up the class path, and similarly for java.library.path if your application requires native code.
  • pass command arguments passed to your main method to the spanwed process

But still, this seems to be a lot of effort that at best represents a leaky abstraction, when instead simple and clear documentation would suffice.

like image 194
mdma Avatar answered Sep 22 '22 07:09

mdma