OutOfMemoryError Sample Code
Package com.walson.heap;
Import java.util.ArrayList;
Import java.util.List;
/**
* java Heap overflow
*
*-verbose:gc-xms20m-xmx20m-xx:+heapdumponoutofmemoryerror
* @author gjh1
*
*/
public class Heapoom {
Static Class oomobject{
}
public static void Main (string[] args) {
list<oomobject> list = new arraylist
while (true) {
List.add (New Oomobject ());
}
}
}
Java Virtual machine settings
Description
- -XMS: Minimum heap memory
- -XMX: Max heap Memory
- Set-XMS to-xmx as 20M is to avoid heap memory knowing the extension
- -xx:+heapdumponoutofmemoryerror : A memory overflow exception occurred when the VM was dumped out of the current memory heap dump snapshot for post-mortem analysis. (under project folder)
Run Results [GC (Allocation Failure) 5504k->3237k (19840K), 0.0108051 SECS][GC (Allocation Failure) 8741k->7223k (19840K), 0.0123580 SECS][GC (Allocation Failure) 12727k->12727k (19840K), 0.0179589 secs][full GC (Allocation Failure) 18231k-& gt;12016k (19840K), 0.0451467 secs][full GC (Allocation Failure) 14714k->14711k (19840K), 0.0540938 secs][full GC ( Allocation Failure) 14711k->14691k (19840K), 0.0588749 Secs]java.lang.outofmemoryerror:java heap spacedumping Heap To Java_pid4352.hprof ... Heap dump file created [26506297 bytes in 0.267 secs]
Description: The heap footprint before recycling and the amount of garbage collected (heap space size) is the last time (in seconds) .
Analysis of memory leaks Storage Analyzer plug-in installation
First, open Eclipse->help->install new software->work with add on the right ...
The dialog box that pops up is as follows
In the Name box, enter the following address in location:
http://download.eclipse.org/mat/1.2/update-site/
The installed version of this address is 1.2.1.
The specific version is http://www.eclipse.org/mat/downloads.php.
Just click OK.
After the decision is made, an interface for selecting the installation appears. Select All. Go on to the next step.
Parse dump File open heap memory dump file
Pie chart Analysis
The whole heap14.3m can be seen from a pie chart, with a dark area of 13.9M, accounting for 94.71%, and a suspected dark area as a memory leak object. The memory is accumulated in one instance of "java.lang.object[ " and "Loaded by " <system class loader> are described below the memory System class loader in the dark area is used to accumulate an object array.
Detailed analysis
Dominator tree as an object-relational diagram
Calculation of shallow heap and retained heap
Shallow size is the amount of memory that the object itself occupies, and does not contain references to other objects, that is, the sum of the object headers plus member variables (not the values of member variables). On a 32-bit system, the object header occupies 8 bytes, and int occupies 4 bytes, regardless of whether the member variable (object or array) refers to another object (instance) or is assigned null, it always consumes 4 bytes. Therefore, for a string object instance, it has three int members (3*4=12 bytes), one char[] member (1*4=4 bytes), and one object header (8 bytes), with a total of 3*4 +1*4+8=24 bytes. According to this principle, for string a= "Rosen Jiang", the shallow size of instance A is also 24 bytes.
Retained size is the object's own shallow size, plus the sum of the shallow size from which the object can be accessed directly or indirectly to the object. In other words, the retained size is the sum of the memory that the object can be recycled to after it has been GC. For a better understanding of retained size, consider an example.
The objects in memory are treated as nodes in, and objects and objects are referenced to each other. Here's a special node GC Roots, Positive solution! This is the beginning of reference chain.
Starting with obj1, the middle blue node represents only objects that can be accessed directly or indirectly through OBJ1. Because it can be accessed through GC roots, the OBJ3 on the left is not a blue node, whereas on the right the picture is blue because it is already contained within the retained collection.
So for the left, the retained size of the obj1 is the sum of the obj4 size of obj1, obj2, Shallow, and the retained size of Obj1, Obj2, Obj3, Obj4 is the sum of shallow size for the right figure. The obj2 retained size can be computed in the same way.
Find out the memory leak code