Online application - High memory usage

Do Java development, often encounter the following two anomalies: 
1, java.lang.OutOfMemoryError: PermGen Space 
2, java.lang.OutOfMemoryError: Java heap Space 
for a detailed explanation of these two exceptions, need to revisit the simple Java Memory model.  
Java memory model is a description of the relationship between the Java program variables (instance fields, static fields and array elements), and the variable stored in the real computer system was removed from memory and memory variables such low-level details.
In the Java virtual machine, the memory is divided into three generations: the new generation (New), of the old generation (Old), on behalf of permanent (Perm).
(1) New Generation New: New objects are stored here
(2) Older Generation Old: New storage migration from the new generation over the life cycle of the longer object. The new generation and the old generation New Old make up the heap memory.
(3) Permanent Generation Perm: non-memory part of the stack. The main storage loaded Class-level object classes such as class itself, method, field and the like.
If java.lang.OutOfMemoryError appears: Java heap space anomaly, indicating that the Java Virtual Machine heap memory is not enough. There are two reasons:
(1) the Java Virtual Machine heap memory settings is not enough, you can adjust the parameters -Xms, -Xmx.
(2) the code to create a large number of large objects, and for a long time can not be collected by the garbage collector (there is cited).
If java.lang.OutOfMemoryError appears: PermGen space, explained that the Java virtual machine on behalf of Perm permanent enough memory settings. Generally this happens, all the program starts to load a large number of third-party jar package. For example: too many applications are deployed in a Tomcat.
From a code perspective, software developers focused java.lang.OutOfMemoryError: abnormal Java heap space, reduce unnecessary object creation, while avoiding memory leaks.
Finally, summarize the investigation methods and techniques of memory failure which:
1, Top command: Linux command. You can view real-time memory usage.
2, jmap -histo: live [pid ], and analysis of specific target number and the size of memory occupied, thereby positioning the code.
3, jmap -dump: live, format = b, file = xxx.xxx [pid], then use the MAT tools to analyze memory leaks and so on.

 Now with a practical example analysis troubleshooting memory footprint.

 Through the top order found for the Java process PID 9004 has been occupied by relatively high memory is not released (24.7%), high memory usage fault occurs.

Previous remembered one line application troubleshooting: High CPU utilization PS command description, the ability to find which specific thread is it?

ps -mp 9004 -o THREAD,tid,time,rss,size,%mem

 

 

 

Unfortunately, the discovery PS command can be found in the specific process CPU utilization, but can not be found under a specific process thread memory occupancy.

 

I had to seek other methods, but fortunately Java provides a good memory monitoring tools: jmap command

jmap command has several common usage the following:

•jmap [pid]

•jmap -histo:live [pid] >a.log

•jmap -dump:live,format=b,file=xxx.xxx [pid]

The latter two are the most used. Which, jmap -histo: live [pid] can view the number of active objects occupy memory and the size of the current Java process creation.

jmap -dump: live, format = b, file = xxx.xxx [pid] the current memory usage Java process can lead out, easy to use specialized memory analysis tool: to analyze (eg MAT).

This command is very helpful to have a memory leak analysis. Specifically how to use this blog can view another article: using the Eclipse Memory Analyzer Tool (MAT) memory leak analysis

Guess you like

Origin www.cnblogs.com/tanxiaojun/p/11924590.html