how to increase java heap memory permanently?

前端 未结 5 1658
南笙
南笙 2020-12-02 23:14

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 2

相关标签:
5条回答
  • 2020-12-02 23:52

    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

    0 讨论(0)
  • 2020-12-02 23:59

    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

    0 讨论(0)
  • 2020-12-03 00:03

    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.

    0 讨论(0)
  • 2020-12-03 00:06

    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

    0 讨论(0)
  • 2020-12-03 00:18

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题