Question

I am launching a java jar file which often requires more than the default 64MB max heap size. A 256MB heap size is sufficient for this app though. Is there anyway to specify (in the manifest maybe?) to always use a 256MB max heap size when launching the jar? (More specific details below, if needed.)


This is a command-line app that I've written in Java, which does some image manipulation. On high-res images (about 12 megapixels and above, which is not uncommon) I get an OutOfMemoryError.

Currently I'm launching the app from a jar file, i.e.

java -jar MyApp.jar params...

I can avoid an OutOfMemoryError by specifying 256MB max heap size on the command line, i.e.:

java -Xmx256m -jar MyApp.jar params...

However, I don't want to have to specify this, since I know that 256MB is sufficient even for high-res images. I'd like to have that information saved in the jar file. Is that possible?

Was it helpful?

Solution

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

OTHER TIPS

Write a batch or shell script containing the following line. Put into the folder, where MyApp.jar is stored.

java -Xmx256M -jar MyApp.jar

After that always open this batch/script file in order to launch the JAR file. Although, This will not embed specification of virtual memory size into jar file itself. But, It can override the problem to write the same command often on command line.

I had a similar need, and thought I should be able to specify this in the manifest as suggested above. However, this is not possible. My solution is similar to what Alex suggested above, however, what I did was to have the main method use Java's ProcessBuilder to start another Java process. When starting another process you can specify the maximum memory for the new process. You are essentially having one Java process start another one. It's a little hokey, but it does work and it still allows you to launch your app by double-clicking the Jar file, which I know you want to do. Look into the ProcessBuilder.

This isn't great, but you could have it as an executable JAR, and then in it's main, have it execute itself via the command-line as a non-daemon thread with the proper params stored in a properties file or calculated or whatever, then exit the original. You could even have it execute the jar with another "real" entry-point that expects those parameters.

Found an answer on google.

He says no for the JAR file, yes in JavaWeb Start, and that you should do it in your (possibly system-specific) launcher/wrapper script/app.

YES! You need to write a small starter program - I wrote a small mostly cross platform compatible (paths should be fine) program to do it - see below. This has been tested on Windows/Ubuntu/Mac OS X.

Silent Development Blog article on increasing executable Java jar heap

Apparently this works on Ubuntu

> export _JAVA_OPTIONS="-Xmx1g"

java -jar jconsole.jar & Picked up _JAVA_OPTIONS: -Xmx1g

There are many ways of doing it:

You can by having another Jar file which triggers your main Jar.

Or, have a batch file which will start your jar. But be careful, if downloaded from say net, the user will have to set permissions for the script to be runnable

Build an installer. On Mac, using the Oracle Java App bundler for Java 7, Apple App bundler for Java 6 build the .app file. You still cant redistribute it as the necessary permissions wont be set. Build a dmg for the app file. This can be used for distribution. A similar installer for Windows

The third technique would be the best, as you can then package the dependencies well, set all JVM arguments etc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top