Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to increase java heap memory permanently?

Tags:

I have one problem with java heap memory. I developed one client server application in java which is run as a windows service it requires more than 512MB of memory. I have 2GB of RAM but when I run my application it throws an exception

Out of memory error:java heap space

but I have already set heap size (maximum 512MB) in the java control panel and I still get the same error. I can't set heap size through the command line because my application runs as a windows service so how can I increase the default heap size?

like image 551
Mr.Cool Avatar asked Jul 20 '12 11:07

Mr.Cool


People also ask

How increase JVM heap size permanently?

Edit the -Xmx256m option. This option sets the JVM heap size. Set the -Xmx256m option to a higher value, such as Xmx1024m.

How do I stop Java heap exhaustion?

If the Java heap is being exhausted, and increasing the Java heap size does not solve the problem, the next stage is to examine the objects that are on the heap, and look for suspect data structures that are referencing large numbers of Java objects that should have been released.

How do I increase allocated heap size?

1)Change the gradle. properties file and change the heap size as per your requirement. 2)"Edit Custom VM Options" from the Help menu.


2 Answers

The Java Virtual Machine takes two command line arguments which set the initial and maximum heap sizes: -Xms and -Xmx. You can add a system environment variable named _JAVA_OPTIONS, and set the heap size values there.
For example if you want a 512Mb initial and 1024Mb maximum heap size you could use:

under Windows:

SET _JAVA_OPTIONS = -Xms512m -Xmx1024m 

under Linux:

export _JAVA_OPTIONS="-Xms512m -Xmx1024m" 

It is possible to read the default JVM heap size programmatically by using totalMemory() method of Runtime class. Use following code to read JVM heap size.

public class GetHeapSize {     public static void main(String[]args){          //Get the jvm heap size.         long heapSize = Runtime.getRuntime().totalMemory();          //Print the jvm heap size.         System.out.println("Heap Size = " + heapSize);     } } 
like image 62
George D Avatar answered Sep 24 '22 03:09

George D


This worked for me:

export _JAVA_OPTIONS="-Xmx1g"

It's important that you have no spaces because for me it did not work. I would suggest just copying and pasting. Then I ran:

java -XshowSettings:vm

and it will tell you:

Picked up _JAVA_OPTIONS: -Xmx1g

like image 35
Justin Chitla Avatar answered Sep 24 '22 03:09

Justin Chitla