Packagecom.xiaoysec.test;Importjava.util.ArrayList;Importjava.util.List;/***VM args:-verbose:gc-xms20m-xmx20m-xmn10m-xx:+printgcdetails-xx:survivorratio=8 *@authorXiaoysec **/ Public classHeapoom {Static classoomobject{//define a static inner class } Public Static voidMain (string[] args) {List<OOMObject> list =NewArraylist(); //int i=0; while(true) {List.add (Newoomobject ()); //System.out.println (i); //i++; } }}
The code is fairly simple, which is a heapoom class for heapoutofmemory testing, in which there is a static inner class Oomobject
The main idea is to use a dead loop to create objects in the main method. We know that the Java object is in the Java heap when the heap memory is low when the virtual opportunity to garbage collection, but when the virtual machine found that these objects are still useful can not be recycled, expand the heap space, But in this case there is a while (true) which means that even expanding the space is useless!
It is worth mentioning that we need to set-verbose:gc-xms20m-xmx20m-xmn10m-xx:+printgcdetails-xx:survivorratio= in VM arguments 8 that is, set the maximum minimum value of the heap to the same 20M to prevent the heap from automatically expanding
Here is the result of the operation:
[GC (Allocation Failure) [psyounggen:7882k->1000k (9216K)] 7882k->5433k (19456K), 0.0100414 secs] [times:user=0.05 sys=0.00, real=0.01 secs]
[GC (Allocation Failure)--[psyounggen:9192k->9192k (9216K)] 13625k->19424k (19456K), 0.0155543 secs] [times:user=0.05 sys=0.00, real=0.02 secs]
[Full GC (ergonomics) [psyounggen:9192k->0k (9216K)] [paroldgen:10232k->10051k (10240K)] 19424k->10051k ( 19456K), [metaspace:2756k->2756k (1056768K)], 0.2144901 secs] [times:user=0.36 sys=0.00, real=0.21 secs]
[Full GC (ergonomics) [psyounggen:7635k->8076k (9216K)] [paroldgen:10051k->7940k (10240K)] 17687k->16017k ( 19456K), [metaspace:2756k->2756k (1056768K)], 0.1848934 secs] [times:user=0.37 sys=0.00, real=0.18 secs]
[Full GC (Allocation Failure) [psyounggen:8076k->8076k (9216K)] [paroldgen:7940k->7940k (10240K)] 16017k-> 16017K (19456K), [metaspace:2756k->2756k (1056768K)], 0.1011278 secs] [times:user=0.33 sys=0.00, real=0.10 secs]
Exception in thread "main" Java.lang.OutOfMemoryError:Java Heap space
At Java.util.Arrays.copyOf (Unknown Source)
At Java.util.Arrays.copyOf (Unknown Source)
At Java.util.ArrayList.grow (Unknown Source)
At Java.util.ArrayList.ensureExplicitCapacity (Unknown Source)
At Java.util.ArrayList.ensureCapacityInternal (Unknown Source)
At Java.util.ArrayList.add (Unknown Source)
At Com.xiaoysec.test.HeapOOM.main (heapoom.java:20)
[Full GC (ergonomics) [psyounggen:8192k->0k (9216K)] [paroldgen:7940k->656k (10240K)] 16132k->656k (19456K), [ metaspace:2781k->2781k (1056768K)], 0.0056287 secs] [times:user=0.01 sys=0.00, real=0.01 secs]
Heap
Psyounggen total 9216K, used 82K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
Eden Space 8192K, 1% used [0x00000000ff600000,0x00000000ff614938,0x00000000ffe00000)
From space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
To space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
Paroldgen total 10240K, used 656K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
Object Space 10240K, 6% used [0x00000000fec00000,0x00000000feca4310,0x00000000ff600000)
Metaspace used 2787K, capacity 4486K, committed 4864K, reserved 1056768K
Class space used 307K, capacity 386K, committed 512K, reserved 1048576K
Java Heap overflow little chestnuts