————— the next day —————
— — — — — —
Let’s look at these three questions together.
Question 1: What needs to be recycled?
First of all we need to know how and what waste needs to be recycled? There are two algorithms for determining whether an object needs to be reclaimed. One is reference counting algorithm and the other is reachability analysis algorithm.
Reference counting algorithm
The reference-counting algorithm is simple. It counts the number of times an object is referenced to determine its importance. If the object is referenced by another object, its reference count is increased by one. If a reference to the object is deleted, its reference count is decreased by one. When the object’s reference count reaches zero, the object is reclaimed.
What’s wrong with reference counting? When two objects refer to each other, the count is not zero because they refer to each other, which causes the two objects to be unrecyclable.
So, the Java virtual machine uses another method to determine whether an object is alive or not, which is the reachability analysis algorithm.
Accessibility analysis algorithm
The reachability analysis algorithm firstly determines a series of GC Roots, and searches a Reference Chain based on the Reference relationship between objects from the root object as the starting point. Objects in the Reference Chain will survive, while those not in the Reference Chain will be identified as recyclable objects.
An apt analogy is that a reachable analysis algorithm is like cleaning a bunch of grapes. We can lift a bunch of grapes from a branch like a chain of references, while objects that are not connected to the chain of references are like grapes scattered in a pool that can be recycled.
- Objects referenced in the virtual machine stack (variables, parameters, and so on used by the running method)
- Objects referenced by class static properties in the method area (fields declared by the static keyword)
- The object referenced by the constant in the method area (that is, the field declared by the final keyword)
- Objects referenced in the native method stack (native methods)
- Internal references of the Java VIRTUAL machine. (Things inside the system can of course be used as roots)
Question 2: What are the important garbage collection algorithms?
Having learned to determine which garbage in memory needs to be collected, we need to master several important garbage collection algorithms.
Mark-clear algorithm
Mark-sweep is the most basic garbage collection algorithm, generally divided into two steps:
- tag
Mark all objects that need to be recycled (gray), which is to do garbage determination.
- remove
Remove the gray areas.
It should be noted that: the so-called clearing does not really need to zero the entire memory byte operation, only need to record the start and end address of the free object into the free list, indicating that this section of memory is free.
- The advantage is that it’s fast and you just need to make a mark to know which piece needs to be recycled, but the disadvantage is also deadly.
- Its main disadvantages are two: one is the unstable execution efficiency, and the other is the problem of memory fragmentation.
One might ask, what does fragmentation mean? Although the stack described above is cleared through the mark clearing method, the cleared memory is a large number of discontinuous memory fragments. For example, the object below, although the whole object has a bit, it cannot be put in because of the discontinuity, which is the biggest disadvantage of the mark – clear algorithm.
The so-called tag copying algorithm and tag sorting algorithm are both improvements on the shortcomings of the tag clearing algorithm, so it is said that the tag clearing algorithm is the most basic way.
Mark-collation algorithm
Unlike the mark-clean algorithm, the mark-clean algorithm is mobile. It moves all surviving objects towards one end of the memory space and then clears out memory beyond the boundary.
- tag
- mobile
What are the drawbacks? The mark-collation algorithm involves moving objects, and during the collation phase, references need to be updated as available objects are moved. It’s inefficient.
Mark-copy algorithm
The mark-copy algorithm, unlike the previous comparison, divides the memory space into two parts. During garbage collection, the living objects in the used memory are copied to the unused memory block, and then all objects in the used memory block are cleared. Finally, the two memory roles are swapped and garbage collection is completed.
Generally speaking, it can be divided into the following steps:
- copy
- empty
- translocation
It is not difficult to see that the efficiency of the tag replication algorithm is improved by eliminating the need for tags. In addition, he will not parameter the fragmentation problem.
But. The disadvantage of the tag copy algorithm is also obvious. It requires double space.
Question 3: What is the specific process of garbage collection?
Instead of using one algorithm alone, the JVM uses a combination of three algorithms to make them work together. The implementation is generational garbage collection in the Java VIRTUAL machine.
This is the partition of Java heap memory. Why do we have to do this? That’s because our Java objects all have different lifetimes, and some may need to be used for a long time, while others may be used and thrown away. Therefore, we can carry out different garbage collection strategies according to different characteristics of its life cycle.
In general, the new generation of garbage collection is more frequent, the old generation of garbage collection triggered a long time. The new generation is dealing with objects that are going to die, while the old generation is recycling objects that are more valuable and will live for a long time.
For example, it is easy to understand: the new generation of garbage disposal, just like the daily garbage disposal, while the old generation of garbage disposal, more like the Spring Festival cleaning, home is too much garbage to clean again. Cleaning up the garbage, are stored in the home for a long time, often may have been very useful, now retired first put to clean up the New Year.
Every time we create an object, it takes up a certain amount of memory in the Garden of Eden, and gradually the garden becomes full. When we try to create objects again, we run out of space.
At this point, a garbage collection is triggered, and the new generation of garbage collection is called MinorGC.
When MinorGC is triggered, the Eden district does an accessibility analysis of each object to know which objects should be removed as garbage.
MinorGC is using the tag copy algorithm here, which stores useful objects in the survival zone to, and then purges the Eden objects.
Those lucky enough to make it into the survivor zone will be marked with a “lucky number” representing how many cleanups they have resisted.
Finally, the surviving zone to and the surviving zone from need to interact with each other. This is not a physical location swap, but rather, their definitions have been swapped, and next time the one on the left is the surviving zone to, and the one on the right is the surviving zone from.
In other words, the survival zone to is always empty.
Let’s do a few more simulations to deepen the impression:
If more objects come in, Eden is full again!
That will trigger a MinorGC, which will move the survivor to the survivor zone and empty everything else.
Finally, don’t forget that the survivor from and survivor to will have to switch places again.
.
Like this article friends, welcome to pay attention to the public number programmer xiao Grey, watch more exciting content