tags: Deep understanding of JVM Java jvm
Add a reference counter to each object. When a reference points to an object, the counter value is increased by one. When a reference fails, the counter is decreased by one. It is judged whether the counter is 0 and whether the object is available.
problem:
It is difficult to resolve mutual references between objects.
Using the GC Roots object as the starting point, start searching downwards. When an object is not connected to the GC Roots by any reference chain, it proves that the object is unavailable.
Java language can be used as GC Roots, including the following types: Why?
The virtual machine will not actively reclaim the objects in the above four areas. So it is suitable for GC Roots object.
note:
The above judges whether the object is dead, the references mentioned are all strong references
Is not
Usually an object is judged to be dead and retrievable, it takes at least two marking processes:
1. When the object GC Roots is unreachable, it is marked for the first time and a filter is performed, the condition is whether it is necessary for the object to execute the finalize() method.
2. If the object does not cover the finalize() method, or the finalize() method has been called by the virtual machine, it is determined that it is unnecessary to execute, and the object will be directly marked as dead and recyclable.
If it is necessary to execute the finalize() method, the object is placed in an F-Queue queue and the finalize() method is executed. If the object is in the process of executing the finalize() method,
If there is a reference to him, the object will be removed from the collection that is about to be recycled when it is marked the second time.
The finalize() method of each object will only be automatically called once by the system.
The mark-and-sweep algorithm is divided into two steps:
1. Mark: mark out the recyclable objects. The method uses the reachability analysis algorithm. (1. The GC is unreachable. 2. The finalize() method is executed, and the object does not escape)
2. Clear: Collect all marked objects uniformly after marking.
Disadvantages:
1. The two processes of marking and clearing are not efficient.
2. A large number of non-contiguous memory fragments will be generated after clearing. (When the program needs to allocate a large object, such as an array, it cannot find enough contiguous memory and has to trigger another garbage collection in advance)
The new generation is divided into 3 regions, eden, from survivor (s0), to survivor (s1), and the capacity is usually 8:1:1.
* The replication algorithm occurs in the new generation;
* The new generation is divided into three regions: eden, s0, and s1;
* S0 and S1 only use one of the areas each time GC;
* When minor GC occurs, copy the surviving objects in Eden and s0 to s1, and then exchange s1 and s0. Ensure that a survivor area is empty every time;
* Objects in the survivor area will have a surviving age of +1 each time they pass a minor GC. Objects in the new generation will be copied to the old generation after their age is greater than a certain value (default 15, configurable).
problem:
What should I do if the S1 space is not enough to store the data of eden and S0 after minor GC?
These objects will enter the old age through the distribution guarantee mechanism.
advantage:
High efficiency, no memory fragmentation.
Disadvantages:
1. Low space utilization. There is always a survivor area empty.
2. When the object survival rate is high, more copy operations are required.
supplement
In most cases, objects will be allocated first in the Eden area of the young generation. When there is no space in the Eden area, minor GC will be triggered. When the survivor area cannot be put down, the object from Eden. Transfer to the old age through the distribution guarantee mechanism.
The so-called large object refers to an object that requires a large amount of continuous space. Such as array objects.
In the new generation, the age of the still-surviving object will be +1 after every minor GC. When the age is greater than a certain value (default 15, configurable), it will enter the old age.
The total size of all objects of the same age in the survivor space is> half of the survivor space, and objects with an age greater than or equal to this age will enter the old age.
When the objects in the Eden area of the new generation need to enter the old generation due to the survivor area cannot fit,
First, determine whether the maximum available continuous space in the old generation is greater than the total space of all objects in the new generation. If it is larger than the Eden area, the Minor GC is safe.
Otherwise, it depends on whether the virtual machine allows the guarantee to fail, and if allowed, continue to check whether the maximum continuous space of the old age is greater than the average size of the objects promoted to the old age, and is greater than the minor GC that can be tried,
Less than or not allowed to guarantee failure. At this time, the old generation needs to perform a Full GC.

Mark the recyclable objects first, and then move the surviving objects to one end to clear the memory outside the boundary.
Disadvantages:
Adding sorting on the basis of the mark-and-sweep algorithm requires data movement. higher cost.
advantage:
No memory fragmentation
According to the different life cycle of the object, the heap is divided into the young generation and the old generation. In this way, suitable recovery algorithms can be selected according to the different characteristics of each generation.
Efficiency: Copy> Mark Sorting> Mark Clear (The efficiency here is just a simple comparison of time complexity, the actual situation may not be the case)
Memory cleanliness: copy = mark order> mark clear
Memory utilization: mark sorting> mark clearing> copy
First, the reference counting method Add a reference counter to the object. Whenever there is a place to reference it, the counter value is incremented by one; when the reference fails, the counter va...
How does JVM determine that an object is garbage? JVM judges that there are two methods of garbage collection, one is a reference count method, and the other is a reachability analysis method. Let's t...
I. Reflections on garbage collection Garbage collection is not a production partner in Java language. In fact, the history of garbage collection is far more than Java, and LISP, in 1960, is born in MA...
How does JVM determine that the object is dead? Foreword First, the reference counter method (abandoned) Second, the reachability analysis What can I do GC ROOTS? Foreword "Reference in-depth und...
As we all know, we are currently the most commonly used virtual machine hotspot using reachability analysis for garbage collection, and reachability analysis need to rely on GC Root. Now I come to be ...
table of Contents How does the GC determine that the object can be recycled? What are the GC Roots? What are strong references, soft references, weak references, and phantom references? The cause of O...
Reference counter algorithm Reference counter algorithm is implemented by a counter: - add a reference to each object counter - if a new reference, the reference counter is incremented by one - If you...
1 to determine how the object is garbage? There are two classical determination methods borrowed FIG users (final text has given link): Reference counting, the idea is very simple, but if there is a c...
As we all know, Java frees programmers from memory management, so that we do not have to manually allocate and free memory when writing code. The task of memory management is taken over by the JVM. Th...
As we all know, Java frees programmers from memory management, so that we do not have to manually allocate and free memory when writing code. The task of memory management is taken over by the JVM. Th...