How to increase JAVA Heap Size?

I need to increase the default java heap size to get OpenNMS Linkd service running smoothly, and fortunately I have 4/5 of my total installed memory free at any given time even when their is maximum load on the server. still java is not performing to it’s bast. hence I need to increase the default java heap size.

Can anyone tell me how to do it?

I’m using Ubuntu 12.04 lts & total memory installed is 16 GB out of which generally 2GB to 4GB is used.

I have already checked below questions and they did not help me resolve my issue.

  1. How to increase memory allocated to java? java.lang.OutOfMemoryError: Java heap space

  2. Comparing Virtual Memory Size with Resident Size

  3. How to increase memory allocated to java? java.lang.OutOfMemoryError: Java heap space

Please feel free to edit or comment for any additional details required.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You could use java -Xmx4096m in order to set your heap to 4 GB.

you could then add export _JAVA_OPTIONS=-Xmx4096m to your shell by their (.bashrc, .zshrc) file.

Solution 2

Hmmmm….Java performance is not necessarily tied to the size of the heap size. By increasing the max size you do prevent one source of out of memory errors (there are other things that generate this java error).

The only efficiency is gained by reducing the number of calls to increase stack memory size if you set the preallocated heap to the maximum size.

Hmm…I guess it might also slow down the garbage collector a little bit since it would not have to react as quickly as a smaller heap size.

Both of the above heap size adjustments are available on the java command line.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply