28

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?

3
  • 1
    The settings in the control panel only affect applets and Java Web Start applications so it would not have any effect on your application.
    – assylias
    Commented Jul 20, 2012 at 11:14
  • How are you running java as a windows service? Normally which ever utility you are using to do this allows you to specify command line arguments. Commented Jul 20, 2012 at 11:52
  • @Mr.Cool You should change the accepted answer below. Commented Feb 12, 2019 at 22:07

5 Answers 5

48

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);
    }
}
4
  • 5
    On linux export _JAVA_OPTIONS="..." works. To verify: java -version The first line should report the options, for example: Picked up _JAVA_OPTIONS: -Xms512m -Xmx2048m. Commented Mar 11, 2016 at 15:39
  • I'm not sure why this is, but the Java VM could never be launched for me with multiple heap options (initial and max). Only with 1 option in _JAVA_OPTIONS
    – Hack-R
    Commented Aug 21, 2016 at 15:44
  • _JAVA_OPTIONS works very well and is picked up also by gradle.
    – ady
    Commented Jan 22, 2020 at 21:10
  • Not clear, how the effective Heap Size is related to the parameters. Commented Dec 19, 2023 at 19:32
4

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

3

if you need to increase reserved memory, there are VM parameters -Xms and -Xmx, usage e.g. -Xms512m -Xmx512m . There is also parameter -XX:MaxPermSize=256m which changes memory reserved for permanent generation

If your application runs as windows service, in Control panels -> Administration tools -> Services you can add some run parameters to your service

4
  • can i set environment variable like this "JAVA_OPT=-Xms512m -Xmx512m"
    – Mr.Cool
    Commented Jul 20, 2012 at 11:24
  • not sure about this, never tried it. If you will try it, please post here the result;) I have updated my post, you can add some parameters to windows service
    – Jan Hruby
    Commented Jul 20, 2012 at 11:26
  • 2
    the JAVA_OPTS environment variable is typically specific to the script you're running (like the tomcat startup scripts). You can't assume setting it will have any effect. Use _JAVA_OPTIONS as mentioned below. It works on windows and linux, provided you are using the oracle provided jvm. No guarantee it will work on JRockit or IBM jvm's.
    – Matt
    Commented Jul 20, 2012 at 13:42
  • 1
    @ Jan Hruby i have tried in another way . i wrote service wrapper for my application and specified the max and min Java heap in service wrapper config now it's working fine, thanks for your valuable comments.
    – Mr.Cool
    Commented Jul 24, 2012 at 6:11
1

You also use this below to expand the memory

export _JAVA_OPTIONS="-Xms512m -Xmx1024m -Xss512m -XX:MaxPermSize=1024m"

Xmx specifies the maximum memory allocation pool for a Java virtual machine (JVM)

Xms specifies the initial memory allocation pool.

Xss setting memory size of thread stack

XX:MaxPermSize: the maximum permanent generation size

-5

Please note that increasing the Java heap size following an java.lang.OutOfMemoryError: Java heap space is quite often just a short term solution.

This means that even if you increase the default Java heap size from 512 MB to let's say 2048 MB, you may still get this error at some point if you are dealing with a memory leak. The main question to ask is why are you getting this OOM error at the first place? Is it really a Xmx value too low or just a symptom of another problem?

When developing a Java application, it is always crucial to understand its static and dynamic memory footprint requirement early on, this will help prevent complex OOM problems later on. Proper sizing of JVM Xms & Xmx settings can be achieved via proper application profiling and load testing.

2
  • 32
    While what you are saying is not wrong, this should not be the accepted answer because it does not answer the OP's question at all. Commented Dec 5, 2014 at 0:23
  • 2
    Yes, this "answer" completely neglects the possibility that the user wishes to regularly run jobs requiring large amounts of data in memory... nothing to do with memory leaks. Commented Apr 4, 2017 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.