Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase java heap size programmatically

I have a java desktop application for searching files and it is usually reaching the default heap limit pretty soon. I wont have access to all the systems it will be installed in so I want to increase the JVM heap size in the application itself. Can anybody help me how can I do that programmatically in my application

like image 395
hrl Avatar asked Jan 15 '10 18:01

hrl


People also ask

How do I increase the process heap size?

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.

How do I free up heap space?

The execution thread that is responsible to clear the heap space is the Garbage Collector. The task of the Garbage Collector is to find all objects that are not referenced at all and reclaim their space. Usually, a Garbage Collector is being executed periodically by the JVM, in order for new space to be created.

What is JVM heap size?

When the JVM is started, heap memory is created and any objects in the heap can be shared between threads as long as the application is running. The size of the heap can vary, so many users restrict the Java heap size to 2-8 GB in order to minimize garbage collection pauses.


2 Answers

Setting -Xmx to one gig doesn't mean that the JVM will allocate that much memory upon start-up. The JVM will allocate only -Xms (plus overhead) until more heap space is needed. Do you need to protect your users from thrashing virtual memory or a failed memory allocation from the OS? If not, just set Xmx to a large value. Note that Windows 32bit JVMs will often ignore Xmx settings greater than 1.2Gig, so it's best to not request more than a gig or so to be safe.

like image 55
ShabbyDoo Avatar answered Sep 20 '22 07:09

ShabbyDoo


There is no such standard API to do so.

I would suggest you use Java Web Start to invoke your application (can be used for local applications to in the latest Sun Java 6) as it allows you to specify values for this.

You can then have three or four links each pointing to exactly the same files but with "Tiny", "Medium", "Large", "Gigantic" heap sizes.

like image 22
Thorbjørn Ravn Andersen Avatar answered Sep 24 '22 07:09

Thorbjørn Ravn Andersen