Java.lang.OutOfMemory summary analysis

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The OOM analysis

I believe there is some experience in java development people more or less have problems with the OutOfMemoryError, this problem has troubled me for a long time, along with the exploration and the accumulation of experience to solve all the problems of the root of the problem, finally has a more in-depth understanding of. Some solutions according to the Internet, in order to be here.
Before solve overflow memory Java problems, the need for JVM (Java virtual machine) memory management have certain understanding. JVM management memory consists of three kinds of memory regions of different types: Permanent Generation space (permanent preservation areas), Heap space (a heap region), Java Stacks (Java stack).
Permanent preservation areas are mainly stored Class (class) and Meta, Class was the first time when the Load is placed in the PermGen space region, Class needs to store the content mainly includes the methods and static attribute.
Examples of heap area is used to store the Class (object), the object needs to be stored is the main content of the non static attribute. Each new used to create an instance of an object, the object instance is stored in the heap area, this part of space has also been management garbage collection mechanism in JVM.
The Java stack with most programming languages including stack of assembly language functions, like input and output parameters of main types of the variables and the methods. There is a separate stack are each thread in Java program.

Prone to memory overflow problem of memory space include: Permanent Generation space and Heap space.

The first OutOfMemoryError: PermGen space

The occurrence of this problem are a large number of jar or class procedures used in the Java virtual machine, not enough class loading space, associated with the Permanent Generation space. To solve this kind of problem has the following two ways:

1. Increase in the Java virtual machine XX:PermSize and XX:MaxPermSize parameter, where XX:PermSize is the initial permanent preservation area size, XX:MaxPermSize is the largest permanent preservation area size.

As for the tomcat6.0, the or catalina.bat file in a series of environment variable names that end (about 70 lines or so) to add a row: JAVA_OPTS=" -XX:PermSize=64M -XX:MaxPermSize=128m"

If the windows server can be set up in the system environment variables. Feel Tomcat released sprint+ struts+ Hibernate architecture program is very easy to occur the memory overflow error. Using the above method, I succeeded in deploying SSH projects Tomcat server downtime problems often.

2. Jar web-inf/lib cleaning applications.

If Tomcat is deployed to multiple applications, many applications use the same jar, the common jar moved to Tomcat lib, reduce the repeated loading class. This method is recommended for people online part, I have not tried, but the feeling is reduced too much space, the first method is the most reliable.

Second OutOfMemoryError: Java heap space

The reason for this problem is the object of the Java virtual machine to create too much, between garbage collection, allocation of virtual machines to heap memory space is full, associated with Heap space. To solve this kind of problem has two kinds of ideas:

1. Inspection procedures, to see whether there is a cycle of death or unnecessary duplication of create many objects. Find the reason, modify the program and algorithm.

2. Increase in the Java virtual machine Xms (initial heap size) and Xmx (maximum heap size) parameter. Such as: set JAVA_OPTS= -Xms256m -Xmx1024m

Third OutOfMemoryError: unable to create new native thread

This error is likely to occur in the Java thread number many, I haven't met, occurrence of intent and solution can reference:


How to view the JVM memory settings information code

1, Runtime.getRuntime().maxMemory();
The maximum available memory, corresponding to -Xmx; maximum memory available to JVM, can be set via -Xmx, the default value if value is 1/4 of physical memory, can not be higher than the physical memory for the computer;
2, Runtime.getRuntime().freeMemory();
The currentJVMFree memory;The currentJVMFree memory,BecauseJVMOnly in the memory to occupy physical memory usage,SofreeMemory()ThevalueIn general is very small,WhileJVMThe actual available memory is not equal tofreeMemory(),Instead, etc.maxMemory()-totalMemory()+freeMemory().And settingJVMMemory allocation.
3, Runtime.getRuntime().totalMemory();

The total number of JVM memory, value memory and freeMemory; equivalent to the current JVM is used (total). For the total current JVM, memory occupied by the value equivalent to the memory and freeMemory JVM is using the sum of (), will increase with the increase of memory used by JVM;

By modifying the configuration of no use, how to do?

1, Modify the INI file
In the eclipse.ini file the default settings changed to -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M or more, depending on your machine's memory allocation and;
2, Modify the configuration in IDE
Open the eclipse, select the Window--Preferences... In the left side of the dialog double-click the Java tree, Then double click on the Installed JREs, In the choice of the right front JRE mark, Then click on the right side of the "Edit" button, A Edit JRE dialog, Enter the -Xms128m -Xmx512m in the Default VM Arguments: box, so set the Java to virtual memory minimum is 128M, The maximum is 512M, Then click "OK" to close the Edit JRE dialog, Then click "OK" to close the Preferences dialog box,
3, Changes in the Shortcut tab
If this does not solve, Right click the eclipse shortcut, In the attribute---The Shortcut tab---The target inputD:\eclipse\eclipse.exe -clean -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M,Among themD:\eclipse\eclipse.exeIseclipsePosition, You can.


Add the parameters

-vmargs: Behind that is VM (Myeclipse virtual machine) parameters
-Xms128m: The minimum heap memory occupation of the system for the 128M virtual machine
-Xmx512m: Virtual machine takes the system the maximum heap memory for 512M
-XX:PermSize=128m: The minimum heap size (minimum permanent preservation areas). The general report of memory, is that this is too small, heap space remaining less than 5% will be warned, suggest that this slightly with a little, but depending on their machine memory size to set.
-XX:MaxPermSize=256m: The maximum heap size (maximum permanent preservation areas). This is suitable for larger.
Note: -Xmx512M 5% 25.6M, value -Xmx request number theory; and the -XX:MaxPermSize must be greater than 25.6M
Therefore, in accordance with the above methods appropriate to modify, below is my computer configuration:

How to monitor the JVM using jvisualvm memory?

Can't add memory to solve the problem, the best regular check program for memory consumption, in order to avoid potential memory leaks. Suggest that you use the jvisualvm to monitor JVM. (JDK own, the command line can be directly jvisualvm), below is a screenshot from tools, reference

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Angelia at December 20, 2013 - 3:12 AM