How to Set Java Max Heap Size for Running from a Jar File

Can I set Java max heap size for running from a jar file?

you could also use a wrapper like launch4j which will make an executable for most OS:es and allows you to specify VM options.

How to set the -Xmx when start running a jar file?

try java -Xmx1024m filename.

I found this on StackOverflow What does Java option -Xmx stand for? and use it when I start Netbeans for instance.

use it like this

java -Xmx1024m -jar JavaApplication.jar

info:
-Xmxn
Specify the maximum size, in bytes, of the memory allocation pool. This value must be a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. The upper limit for this value will be approximately 4000m on Solaris 7 and Solaris 8 SPARC platforms and 2000m on Solaris 2.6 and x86 platforms, minus overhead amounts.

Specifying JVM heap sizes in JAR

There aren't any properties in the manifest specifications that you can use to control heap space. Instead, you can write a launcher main method in your application that executes a second process with your real main method using one of the Runtime.exec implementations. Since you have access to the java.home system variable, you can use the version of Java that's running your launcher to also launch your game.

// Fix to use the version appropriate to the OS
String javaPath = System.getProperty("java.home") + "\bin\java.exe";
Runtime.exec("\"" + javaPath + "\" mygame.jar <heap_args>");

Using java.home to get the Java path avoids path issues. This will also give you control over launching in the future if you decide to change how your program is launched. For example, this can be changed to use Process so that you can also wait for the process to terminate in your launcher, giving you control over post-run cleanup when the game's JVM is fully terminated.

Increase heap size in Java

You can increase to 2GB on a 32 bit system. If you're on a 64 bit system you can go higher. No need to worry if you've chosen incorrectly, if you ask for 5g on a 32 bit system java will complain about an invalid value and quit.

As others have posted, use the cmd-line flags - e.g.

java -Xmx6g myprogram

You can get a full list (or a nearly full list, anyway) by typing java -X.

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

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.

Increase heap size in Java

You can increase to 2GB on a 32 bit system. If you're on a 64 bit system you can go higher. No need to worry if you've chosen incorrectly, if you ask for 5g on a 32 bit system java will complain about an invalid value and quit.

As others have posted, use the cmd-line flags - e.g.

java -Xmx6g myprogram

You can get a full list (or a nearly full list, anyway) by typing java -X.

How to create an executable jar file with 1 GB heap

 java -Xms1200m -Xmx1300m -jar FILENAME.jar

Define this somewhere in the jar File or in the Manifest is not possible:

Can I set Java max heap size for running from a jar file?

How to set Heap Space for non executable JAR using JNI

I found the answer. I was using JNI so before creating JVM we must fill JavaVMOption parameter as "-Xms256m" for minimum size and "-Xmx512m" for maximum size. It will allocate minimum JVM heap size as 256 MB and maximum JVM heap size as 512 MB.

So this is the code to help others:

JavaVMInitArgs args;
JavaVMOption options[3];
args.nOptions = 3;

options[0] = (char*)"-Xms256m";
options[1] = (char*)"-Xmx512m";
options[2] = //Your JAR file path.
args.options = options;

Then pass args (JavaVMInitArgs object) while creating JVM. Above code will set minimum and maximum heap size.

Remember 1 more thing do not set your minimum heap size before setting maximum heap size because in that case your minimum heap size will be more than or equal to maximum heap size and it will crash.



Related Topics



Leave a reply



Submit