Deep understanding of Java Virtual Machine-Chapter 3: deep understanding of Java Virtual Machine
1. Overview
2. Is the object dead?
Reference Counter
Add a reference counter to the object. When there is a reference, add 1 to the counter. When the reference is invalid, the counter minus 1. Objects whose counter is 0 at any time cannot be used again.
If the object is a circular reference, it cannot be processed. JVM is not used.
Accessibility Analysis Algorithm
Using a series of objects called "GC Roots" as the starting point, you can start to search down from these nodes. The paths searched through are referred to as Reference Chain ), when an object is not connected with any reference chain, it indicates that the object is unavailable.
In Java, the objects that can be used as GC Roots include:
Objects referenced in the Virtual Machine stack (the local variable table in the stack frame.
Objects referenced by class static attributes in the method area.
Objects referenced by constants in the method area.
Objects referenced by JNI (Native method) in the local method stack.
References in Java:
Strong Reference is similar to "Object obj = new Object ()". If a Strong Reference still exists, the Object will never be recycled.
Soft Reference is used to describe some useful but not necessary objects. This type of object will be recycled before the system will overflow the memory. If this collection does not have enough memory, a memory overflow exception will be thrown. SoftReference class.
Weak Reference is a non-essential object, but its strength is weaker than that of soft Reference. When the garbage collector is working, such references are recycled regardless of whether the memory is sufficient or not. WeakReference.
A Phantom Reference refers to a ghost Reference or Phantom Reference. The Weakest Reference relationship does not affect its survival time, nor can it be used to obtain an object instance through a virtual Reference, the only purpose is to receive a system notification when the object is recycled by the Collector, which is implemented using PhantomReference.
3. Garbage collection Algorithm
Mark clearing algorithm Mark-Sweep
First, mark the object to be recycled. After the tag is complete, all marked objects are recycled. It may cause fragments.
Copy algorithm Copying
Divide the available memory into two equal-size blocks by capacity, and use one of them each time. When this block is used up, copy the surviving objects to the other, then, clear the used parts.
Memory d fragments are not considered every time the entire half-zone is recycled. The cost is only half of the memory usage.
Currently, commercial virtual machines use this algorithm to reclaim New generations. However, the memory is divided into a large Eden and two smaller shards or, each time, only Eden and one shard or are used, copy the file to another vor at the time of recycling. The default proportion is.
Tag-Sorting Algorithm
Like the Mark-clearing algorithm, the subsequent step is to move all the surviving objects to one end, and finally directly clear the memory outside the end boundary.
Generation Collection Algorithm Generation Collection
Memory is divided into several parts based on different object lifecycles
By default, the ratio of the new generation to the old generation is (you can specify the value through the-XX: NewRatio parameter ).
Eden: from: to = can be specified through -- XX: Using vorratio
4. HotSpot Algorithm Implementation
Enumerate root nodes
To find The reference chain from The GC Root node in The accessibility analysis, you need to pause all Java execution threads (Stop The World, STW ). In the implementation of HotSpot, we use a set of data structures called OopMap to implement it. When the execution system pauses, we do not need to check all the execution context and global reference positions.
Security Point
Security Zone
5. Garbage Collector
Serial collector
A single-threaded collector must pause all other working threads for simplicity and efficiency.
ParNew
In the multi-thread version of Serial, stop other working threads and enable multiple recycling threads to recycle them.
Parallel Scavenge collector
The objective is to achieve a controllable throughput, which is the ratio of the time when the Cpu runs user code to the total CPU consumption time.
Serial Old collector
The old version of the Serial collector.
Parallel Old collector
The Parallel Scavenge collector of the old generation uses multithreading and the "tag-sorting" algorithm.
CMS collector Concurrent Mark Sweep
Collectors with the objective of obtaining the shortest recovery pause time
The initial mark (CMS initial mark), Stop the World, just mark the objects that can be directly associated with GC Roots, the speed is very fast
Concurrent mark (CMS concurrent mark) to perform the GC Roots Tracing process.
CMS remark (CMS remark), Stop the World, to fix the tag record of the part of the object whose tags are changed due to the continued operation of the user program during the concurrent tag
Concurrent cleanup (CMS concurrent sweep)
G1 collector Garbage-first
Spam collector for server applications
1. Concurrency and off-line. With The advantages of multiple CPU and multi-core environments, You can shorten The pause time of Stop-The-World. You can continue to execute The java program through concurrent scripts.
2. Generation-based collection. Different methods are used to manage newly created objects and old objects that have been alive for a period of time and have been subjected to multiple GC operations.
3. spatial integration. On the whole, it is a collector based on the "tag-sort" algorithm. On the part, it is implemented based on the "copy" algorithm and will not generate memory fragments during running, regular available memory is provided after collection.
4. Predictable pause
Young Generation: Serial, ParNew, Parallel Scavenge, G1
Tenured (old) Generation: CMS, Serial Old, Parallel Old
6. Memory Allocation and recovery policies
Objects are preferentially allocated in Eden. When there is not enough space for allocation in the Eden area, a GC is triggered.
Large objects directly enter the Old Age
Long-lived objects in the old age
The Virtual Machine defines an object age counter for each object. If the object is still alive after Eden is born and after the first MinorGC, and can be stored or accommodated, it will be moved to the same vor space, and the object age is 1.
Every time a Minor GC occurs in a vor object, the age increases by 1. When the age increases to a certain level, the object is promoted to the old age.
Dynamic Object age determination
To better adapt to the memory status of different programs, if the total size of all objects of the same age in the same vor space is greater than half of the memory size of the same vorp, objects older than or equal to this age can directly enter the Old Age
Space allocation guarantee