Analysis of the causes of memory overflow problems in JAVA programs

  • 2020-04-01 03:55:42
  • OfStack

This article has analyzed the JAVA program memory overflow problem in detail. Share with you for your reference. The details are as follows:

Meet an online system to Java. Lang. OutOfMemoryError: PermGen space error, need to locate the problem. I don't remember doing this a long time ago, but it's a really interesting question and it's worth looking into. The first reaction, of course, was to add the -xx :+PrintGCDetails parameter to see the specific GC log. However, since the program was started by tomcat, I was worried that it would be difficult to locate too many packages. Since it is under Windows, I'd better use the visualization tool.
Then we have a look at the reasons for this error, on the Internet to find a paragraph of explanation, said very good, post over to borrow :)
  The full name of PermGen space is the Permanent Generation space, which refers to the Permanent storage area of memory. This memory is mainly stored by the JVM for Class and Meta information. Class will be put into PermGen space when loaded. So if you have a CLASS in your application, you are likely to have a PermGen space error, which is common when a web server pre compiles a JSP. This error message is generated if your WEB APP USES a large number of third-party jars that exceed the JVM's default size (4M).
So let's first increase the initial memory size of PermGen:

Under Linux, add the following at the beginning of catalina.sh:

JAVA_OPTS="-Xms1024m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m" 

Under Windows, add the following at the beginning of the file catalina.bat:

set JAVA_OPTS=-Xms1024m -Xmx1024m -XX:PermSize=256M -XX:MaxPermSize=256m 

Then we will use the visual memory view tool to locate the specific problem. The first choice for jdk6 is of course the native tool, the more commonly used are jconsole and jvisualvm (the latter is found to be more powerful because of the rich plug-in support). One of the more bizarre problems I encountered this time was that the tomcat process could not be found after the analysis tool was opened (it turned out that the jre was started, so it should be ok to change the JDK).

Since I can't connect locally, I can connect remotely. Just open JMX.
As above, add it to JAVA_OPTS at the beginning of catalina.sh or catalina.bat files

-Djava.rmi.server.hostname=192.168.1.101 -Dcom.sun.management.jmxremote.port=9000 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

After starting the program, check with netstat that the ports are open correctly to ensure that the remote can connect.
I've been lazy here and I've turned authenticate off. It's a hassle to turn on something that requires some permissions. This setting works on my local PC, but it doesn't work on the server. Maybe some software has been installed to block the port, so I have to make another abasement and change the port to 1000. Open jvisualvm and click file -> Add JMX connection, then add localhost:1000 to connect.

After connecting the program, I observed the memory changes for a period of time. I focused on the Perm situation, which was stable at 94m, and everything was normal for a day. It may be that the previous setting of Perm memory size did not take effect, because the default initialization of Perm is 16m and the maximum is 64m, and the actual consumption may cause this problem, from the current phenomenon, it should not occur again. If you need to further locate the problem, you can also use btrace to see exactly where a method is called. This locates whether certain methods are performing as expected.

I hope this article has been helpful to your Java programming.


Related articles: