Test program
Public classHeapoom {Private Static inti = 0; Static classOomobject {} Public Static voidMain (string[] args) {List<OOMObject> list =NewArraylist<oomobject>(); Try { while(true) {List.add (Newoomobject ()); I++; } } finally { //810325System.out.println (i); } }}
Output:
[GC (Allocation Failure) 5142k->3031k (19968K), 0.0070246secs] [GC (Allocation Failure) 8663K->7062k (19968K), 0.0066110secs] [GC (Allocation Failure) 12694K->12708k (19968K), 0.0091115secs] [Full GC (ergonomics) 12708K->10719k (19968K), 0.1400352secs] [Full GC (ergonomics) 16351K->14231k (19968K), 0.1051886secs] [Full GC (ergonomics) 16799K->16622k (19968K), 0.1139390secs] [Full GC (Allocation Failure) 16622K->16593K (19968K), 0.1047700Secs]java.lang.outofmemoryerror:java heap spacedumping heap to java_pid11700.hprof ... Heap dump file created [28412377 bytes in 0.082secs]810325Exception in Thread"Main"Java.lang.OutOfMemoryError:Java Heap space at Java.util.Arrays.copyOf (Arrays.java:3210) at Java.util.Arrays.copyOf (Arrays.java:3181) at Java.util.ArrayList.grow (Arraylist.java:261) at Java.util.ArrayList.ensureExplicitCapacity (Arraylist.java:235) at Java.util.ArrayList.ensureCapacityInternal (Arraylist.java:227) at Java.util.ArrayList.add (Arraylist.java:458) at Heapoom.main (Heapoom.java:17) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke (Nativemethodaccessorimpl.java:62) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Delegatingmethodaccessorimpl.java:43) at Java.lang.reflect.Method.invoke (Method.java:498) at Com.intellij.rt.execution.application.AppMain.main (Appmain.java:144)
View heap dump files using Java VISUALVM java_pid11700.hprof
You can see that the Oomobject class has 810,326 instances, causing memory overflow oom.
@Java VISUALVM Analysis Heap dump file