15
A Java Virtual machine is a process that conforms to the characteristics of the operating system process and is multithreaded. The entire virtual machine can not be separated from the operating system constraints.
Running the Java virtual Runtime structure diagram:
A process contains multiple threads. Some of the data is shared in the thread.
The Java Virtual machine divides the memory into different regions, some of which exist when the Java Virtual machine is started, and some things exist as threads are generated and destroyed.
Oom-out of Memory
1, the operating system of Oom killer
2. Oom in a Java virtual machine
2 The principle of oom is similar.
//VM parameters:-xms20m-xmx20m-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=/users/root/documents/javadump/Importjava.util.ArrayList;Importjava.util.List; Public classMyjava {Static classoomobject{} Public Static voidMain (string[] args)throwsinterruptedexception {List<OOMObject> list =NewArraylist<oomobject>(); while(true) {List.add (Newoomobject ()); } }}
Operation Result:
/users/root/documents/javadump/java_pid1014.hprof ... Heap dump file created [27569234 bytes in 0.121"main" Java.lang.OutOfMemoryError:Java heap space
When the memory reaches 20M, it will be killed. and dump to the specified directory.
Java Virtual Machine garbage collection
1. What is recycled? What can be recycled.
The previous way to see if the object has no references. If there is no reference, recycle. But like if 5, and 6 are useless, but they refer to each other. Can not be recycled.
Now use the diagram on the left. By maintaining a GC root. All objects can reach the root, which means that they cannot be recycled. If the root cannot be reached, it can be recycled even if the reference is not 0. It's like. The 5,6,7 reference is not 0, but it cannot reach the root and the Java Virtual machine is recycled.
The above program:
Each arrylist is a storage Oomobject object.
In the while loop, it has not finished executing. All objects always point to the root ArrayList. Has not been released, so Oom
If done, it can be released.
The Java virtual machine has been garbage collected many times before oom. Just not finished recycling.
-XX:+PRINTGC add a parameter to print GC information
[GC (Allocation Failure) 4986k->3457k (19968K), 0.0057425 secs]
[GC (Allocation Failure) 8994k->8000k (19968K), 0.0102037 secs]
[Full GC (ergonomics) 16797k->12588k (19968K), 0.1313456 secs]
[Full GC (ergonomics) 16266k->16101k (19968K), 0.1299640 secs]
[Full GC (Allocation Failure) 16101k->16089k (19968K), 0.0809210 secs]
Java.lang.OutOfMemoryError:Java Heap Space
Dumping Heap to/users/root/documents/javadump/java_pid1048.hprof ...
Heap dump file created [27569234 bytes in 0.173 secs]
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space
2. When to recycle
3, specific how to recycle
View Java by JVISUALVM More detailed information on the memory heap immediately
// Run code Public class mytest { publicstaticvoidthrows interruptedexception { System.out.println ("********************************"); Thread.Sleep (1000*60*5); System.out.println ("###############################");} }
Meta-space = method area
Old (older age) +eden (young belt) +s0+s1= heap space.
Each part has its own garbage collection algorithm.
The newly generated objects are generally placed in Eden and S0. Higher mortality rate
General garbage collection is first reclaimed in the Eden area, and if it is not recycled after a lot of recycling, the object will be moved to the old area, which is generally used for long periods of time.
Eden+s0 are frequently used and frequently recycled. Mortality is high. Adopt a replication strategy. is to copy the rest to S1 or to old.
In the old age adopted the recycling strategy, marking-sorting or labeling of cleanup algorithms. Some classes can be recycled, marking them first. If you can die, recycle.
Specify JC. For the jdk1.7 version of
In this figure, if there are no lines attached between the 2 algorithms, the representation cannot coexist. For example, young with the designation parallel scavenge so that the age can only use serial old or parallel old. Cannot use CMS
Stop the World
-xx:+printgcdetails // print GC details // print GC general Information
Run the first program to print GC details. The results of the operation are as follows:
[GC (Allocation Failure) [Psyounggen (Cenozoic): 4995K (pre-recycle)->480k (after recycling) (6144K) (total size)] 4995K (whole before recycling)->3433k (whole after recycling) ( 19968K) Overall heap space XMS and xmx set size, 0.0068095 secs (time of Use)] [times:user=0.02 (System User State) sys=0.00 (System State), real=0.01 (real-time perception). For example, there are 3 CPUs, each CPU accounted for 0.01, the user is 0.03, but parallel at the same time, so the perception of users may only use 0.015)secs] [GC (Allocation Failure) [psyounggen:6017k->496k (6144K)] 8970k->8000k (19968K), 0.0110276 secs] [times:user=0.02 sys=0.01, real=0.01secs] [Full GC (ergonomics) [psyounggen:6128k->0k (6144K)] [Paroldgen (old age): 10669k->12588k (13824K)] 16797k->12588k (19968K), [Metaspace (meta space): 2620k-> 2620K (1056768K)], 0.1348238 secs] [times:user=0.26 sys=0.00, real=0.14secs] [Full GC (ergonomics) [psyounggen:3676k->2488k (6144K)] [paroldgen:12588k->13612k (13824K)] 16265k->16101k (19968K), [metaspace:2620k->2620k ( 1056768K)], 0.1376546 secs] [times:user=0.39 sys=0.01, real=0.14secs] [Full GC (Allocation Failure) [psyounggen:2488k->2488k (6144K)] [paroldgen:13612k->13600k (13824K) (old age increase)] 16101k->16089k (19968K), [metaspace:2620k-> 2620K (1056768K)], 0.1072099 secs] [times:user=0.26 sys=0.01, real=0.11secs] Java.lang.OutOfMemoryError:Java heap spacedumping heap to/users/root/documents/javadump/java_pid1444.hprof ... Heap dump file created [27569234 bytes in 0.135secs] Exception 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 Myjava.main (Myjava.java:11) Heap Psyounggen total 6144K, used 2711K [0x00000007bf980000, 0x00000007c0000000, 0x00000007c0000000) Eden Space 5632K,48% used [0x00000007bf980000,0x00000007bfc25d80,0x00000007bff00000) from Space 512K,0% used [0x00000007bff80000,0x00000007bff80000,0x00000007c0000000) to space 512K,0% used [0x00000007bff00000,0x00000007bff00000,0x00000007bff80000) Paroldgen total 13824K, used 13600K [0x00000007bec00000, 0x00000007bf980000, 0x00000007bf980000) object Space 13824K,98% used [0x00000007bec00000,0x00000007bf9481b0,0x00000007bf980000) Metaspace used 2652K, capacity 4486K, committed 4864K, reserved 1056768KclassSpace used 289K, capacity 386K, committed 512K, reserved 1048576K
Jstat
Importjava.util.ArrayList;Importjava.util.List; Public classMyjava {Static classoomobject{} Public Static voidMain (string[] args)throwsinterruptedexception {List<OOMObject> list =NewArraylist<oomobject>(); Thread.Sleep (15000); while(true) {List.add (Newoomobject ()); } }}
JSTAT-GC PID 1000 (how often to print) 20 (how many times)
JSTAT-GC PID 1000 (how often to print) > a.txt Save to File
s0c s1c s0u s1u EC EU OC OU MC MU ccsc ccsu ygc ygct FGC FGCT GCT 8192.0 8192.0 0.0 0.0 51712.0 2068.5 136704.0 0.0 4480.0 774.4 384.0 75.9 0 0.000 0 0.000 0.0008192.0 8192.0 0.0 0.0 51712.0 2068.5 136704.0 0.0 4480.0 774.4 384.0 75.9 0 0.000 0 0.000 0.000
c Maximum size u use
YGC GC Times
YGCT How much time does the new generation occupy in garbage collection?
GCT GC Total amount of time to take up including YGC FGC
Jstat-gcutil PID 1000 20
Jmap
Can load dump files with VISUALVM
Java Virtual Machine