Preface:
Simulation actual combat screen heap memory (Java. Lang. OutOfMemoryError: Java heap space).
The cause of a heap overflow: A heap overflow occurs when a large number of objects are created that are referred to all the time and cannot be collected by the GC garbage. As a result, the heap becomes full and there is not enough space for the newly created objects.
In actual business scenarios, troubleshooting of memory overflow is usually very difficult and tedious. This article will illustrate the specific thinking and steps of troubleshooting by combining a simple example.
Preparation:
Note: In a real scenario, it is usually projects deployed on Linux servers that report memory overflow problems; In order to restore the actual scenario as much as possible, the code that can trigger memory overflow is also written in advance and packaged into a runnable Jar package, which is then put into the server for execution.
1. Prepare code that causes memory overflow:
// Create a Java class
public class OutOfMemory {
private String test;
public OutOfMemory(String test){
this.test = test; }}// Simulate memory overflow
public class TestOOM {
public static void main(String[] args) {
List<OutOfMemory> list = new ArrayList<OutOfMemory>();
while(true) {/** * Creates OutOfMemory objects indefinitely until the heap is full, and the created OutOfMemory objects are referenced by the list collection objects, which causes the GC to fail to collect them and eventually causes the heap to overflow */
list.add(new OutOfMemory("5656"));
System.out.println("5656"); }}}Copy the code
Once the code is written, export it using development toolsA runtime Jar package– (TestOOM jar) |
2. Prepare the Linux server
You can use centos or Red Hat.
Practical:
1, put the Jar package in the server to execute:
Testoom.jar = TestOOM Jar = TestOOM Jar = TestOOM Jar = TestOOM Jar = TestOOM Jar = TestOOM Jar = TestOOM Jar = TestOOM Jar = TestOOM Jar ② Execute Jar package with command; Command: Java – Xms40m – Xmx70m – XX: XX: + HeapDumpOnOutOfMemoryError – HeapDumpPath = / usr/TMP – jar TestOOM. Jar
Note: To simulate a heap overflow as quickly as possible, some parameters are set when starting the Jar package; Parameter analysis: 1), – Xms40m initial heap size set to 40 m 2), – Xmx70m maximum heap size set to 70 m 3), – XX: + HeapDumpOnOutOfMemoryError the heap memory leak, Dump snapshot 4), -xx :HeapDumpPath=/usr/tmp Set the directory for storing the exported heap snapshot to /usr/tmp
2. Run the following command to monitor JVM information:
JPS command: this command is used to query the process related to Java, and output the process number; Here is the process number of the Jar package running above:
Jmap-heap 3324 Jmap-heap 3324 jmap-heap 3324 jmap-heap 3324 jmap-heap 3324 jmap-heap 3324 jmap-heap 3324 jmap-heap 3324 The diagram below:
In the figure, you can see that the free free space in the new generation and the old generation is getting smaller and smaller in the heap memory, which indicates that GC garbage collection is about to occur, freeing up more space in the heap for newly created objects.
The jstat command is used to monitor JVM performance information. For example, the jstat command is used to monitor the JVM’s GC garbage collection. Jstat -gcutil 3324 1000 jstat -gcutil 3324 1000 jstat -gcutil 3324 1000 The diagram below:
(1) Why do YGC and FGC trigger so quickly? A: Because the heap is running out of memory space, some space needs to be reclaimed through GC garbage collection to hold newly created objects.
(2) What exactly happens to GC when the heap is out of memory? A: 1) When the Cenozoic space in the heap is insufficient, YGC (also known as Minor GC) will be triggered. After garbage collection, the Cenozoic space in the heap will survive in the remaining space
If there are objects whose “promotion age” reaches the “promotion age threshold”, they will be moved to the old generation of the heap for storage. Therefore, after each YGC, the number of objects stored in the old generation of the heap may increase. Note: Age of promotion: the age of promotion is increased by 1 for each YGC performed by a new generation. Note: Promotion age threshold: In Serial and ParNew GC, the “promotion age threshold” can be set with the parameter MaxTenuringThreshold. The default value is 15.
The above “age for advancement” comes from: GC optimization of Java applications from a practical case study
2) When YGC is about to occur in the new generation of the heap, if it is found that the space occupied by the surviving objects in the new generation that reach the “promotion age threshold” is larger than the remaining available space in the old generation of the heap, FGC will be directly triggered instead of YGC. FULL GC collects garbage from the entire heap space (new generation, old generation) as well as the method area/permanent generation. Note: FULL GC can be divided into two main steps: first, the old generation garbage collection (also known as Major GC) and then the new generation garbage collection (YGC).
Extension: Structure diagram of the heap |
If a memory overflow occurs, a snapshot is automatically generated and the heap memory snapshot is analyzed:
① Use XFTP and other tools to export the snapshot file in the server. The heap memory snapshot file is a file with the suffix hprof. After the snapshot file is exported, you can use the JVisualvm. exe analysis tool of the JDK to open it for analysis.
Where is JVisualvm. Exe? (On Windows, for example) it is in the bin directory of the JDK installation directory.
As shown in figure:
2, Use jVisualVM. Exe to import snapshot file, as shown in figure:(2),(3),
By analyzing the heap memory snapshot, the conclusion is as follows: According to figure (3), it can be found that there is one instance object in the heap memory accounting for 99.9%. It can be determined that the heap memory overflow is caused by the massive creation of this instance object. At this point, we can go back to our own little program that can trigger a heap overflow and see that it is the OutOfMemory object created wirelessly in a while(true) infinite loop that causes the heap to run OutOfMemory space.
For example, the small example above automatically generates a heap snapshot file size of more than 100 MEgabytes in the event of a heap overflow. In a real world scenario, this might be very large and the snapshot analysis tool might not be able to import the heap snapshot. Therefore, we need to continue to learn at ordinary times, in order to solve problems as soon as possible when problems occur in the future; Programmers need to be able to solve problems as well as write good code.
Don’t forget to leave your learning footprints
All see the article not praise are “play rascal”, hey hey ヾ(◍°∇°◍) Blue! Just as a joke, move your little hands and click “like”, each of you will make more learners join in with a contribution (like + comment)! Thank you very much!  ̄  ̄ omega =