How to solve JVM memory overflow

  • 2020-04-01 02:40:08
  • OfStack

Java. Lang. OutOfMemoryError: PermGen space

I found that many people attributed the problem to spring,hibernate, and tomcat, because they dynamically generated classes that caused the permanent heap overflow in the JVM. Then there was a lot of talk about how to fix it, with some saying that you could upgrade to the latest version of tomcat or not use tomcat at all. There are also people who are suspicious of spring, and the discussion is intense in the spring forums, because spring's use of CBLIB with AOP generates many classes dynamically.
But the question is why these trumps of open source have the same problem, and is it more fundamental? Tomcat implicitly answered this question in Q&A, which we know, but which was generated by a more basic question.
Someone then did an inspection of the more basic JVM and found the crux of the problem. The original SUN JVMS divided memory into different sections, one of which was the permenter section for the much used classes and class descriptions. SUN designed this area to be fixed when the JVM started, but he didn't expect it to be so widely used now. And this area has a special garbage collection mechanism, the problem now is that after dynamically loading classes into this area, the gc has no way to recover!

1, the first is: Java. Lang. OutOfMemoryError: Java heap space

Explanation:

Heap size setting

The JVM Heap setting refers to the memory space that can be allocated by the JVM during the running of Java programs. The JVM automatically sets the value of Heap size at startup, with the initial space (i.e. -xms) being 1/64 of the physical memory and the maximum space (-xmx) being 1/4 of the physical memory. Options such as -xmn-xms-xmx provided by the JVM can be set. The Heap size is the sum of Young Generation and Tenured Generaion.
Tip: this exception information is thrown in the JVM if 98% of the time is spent in GC and the available Heap size is less than 2%.
Tip: the Heap Size should not exceed 80% of the available physical memory, and the -xms and -xmx options should generally be set to the same, while -xmn is 1/4 of the -xmx value.

Solutions:

Set the Heap size manually
Modify TOMCAT_HOME/bin/catalina.bat to add the following line above "echo "Using CATALINA_BASE: $CATALINA_BASE" :


set JAVA_OPTS=%JAVA_OPTS% -server -Xms800m -Xmx800m -XX:MaxNewSize=256m 

Or modify the catalina. Sh
Add the following line above "echo "Using CATALINA_BASE: $CATALINA_BASE" :


JAVA_OPTS="$JAVA_OPTS -server -Xms800m -Xmx800m -XX:MaxNewSize=256m" 
[html]
2 , followed by: java.lang.OutOfMemoryError: PermGen space 
 The reason:  
PermGen space Is the full name of Permanent Generation space, Refers to the memory of the permanent storage area, this memory is mainly by JVM store Class and Meta The information of ,Class In the Loader Will be put in PermGen space , it and store class instances (Instance) the Heap Different regions ,GC(Garbage Collection) Not during the main program runtime PermGen space Clean up, so if you have a lot in your application CLASS if , It's very likely to happen PermGen space Error, this kind of error is common in web The server to JSP for pre compile From time to time. If you have a WEB APP I'm using a lot of third parties jar,  It's bigger than that jvm Default size (4M) This error message is generated.  
 Solutions:  
1.  manually MaxPermSize The size of the  
 Modify the TOMCAT_HOME/bin/catalina.bat ( Linux for catalina.sh ), 
[code]
 " echo "Using CATALINA_BASE: $CATALINA_BASE" Add the following lines above:  
set JAVA_OPTS=%JAVA_OPTS% -server -XX:PermSize=128M -XX:MaxPermSize=512m 

Catalina. Sh for:


JAVA_OPTS="$JAVA_OPTS -server -XX:PermSize=128M -XX:MaxPermSize=512m"


Related articles: