Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 283
Kip Avatar asked Jun 19 '09 14:06

Kip


People also ask

How can we increase the size of heap in a running process?

Just use a function like malloc() or calloc() to allocate memory dynamically. To deallocate the memory and return it to the heap, use free() . These functions will manage the size of the heap by expanding or shrinking it as needed.


2 Answers

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.

like image 156
kravi Avatar answered Oct 16 '22 03:10

kravi


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

like image 40
willcodejavaforfood Avatar answered Oct 16 '22 04:10

willcodejavaforfood