= = = = = = = = = = = = = =
Book Notes series
= = = = = = = = = = = = = =
Next we will talk about the most common problems, garbage collection algorithms, and memory allocation strategies.
Figure 1. Common garbage collection algorithms
Figure 2. Partitioning of the Java heap and its scale
Figure 3. Memory allocation strategy
Garbage collection algorithm
1. Mark-clear algorithm
The “Mark-swap” algorithm is the most basic collection algorithm, and subsequent collection algorithms are based on this idea and improved on its shortcomings. As the name implies, the algorithm is divided into “mark” and “clear” two stages: first, all the objects that need to be recovered at the mark, that is, the “dead” objects, after the completion of the mark, all the marked objects are uniformly recycled. Its deficiencies mainly have two: one is the efficiency problem, marking and clearing the efficiency of the two processes are not high; Another problem is the space problem. After the mark is cleared, a large number of discontinuous memory fragments will be generated. Too many space fragments may cause that when the program needs to allocate large objects in the future, it cannot find enough continuous memory and has to trigger another garbage collection action in advance.
FIG. 1.1 Schematic diagram of marker clearing algorithm
2. Replication algorithm
In order to solve the problem of efficiency, “Copying” collection algorithms came into being. It divides the available memory into two equally sized pieces according to capacity and uses only one piece at a time. When this block of memory is used up, the surviving objects are copied onto the other block, and the used memory space is cleaned up again. In this way, every time the whole move to carry out memory reclamation, memory allocation does not have to consider the complex situation of memory fragmentation, as long as the heap top pointer is moved, memory allocation can be in order, simple implementation, efficient operation. However, the cost of this algorithm is to reduce the memory in half, which is a bit too high.
Figure 1.2 Schematic diagram of replication algorithm
Commercial virtual machines now use this collection algorithm to recycle the new generation. 98% of objects in the new generation are “live and die”, so there is no need to divide the memory space into a large Eden space and two smaller Survivor Spaces, using Eden and one Survivor at a time. When recycling is done, the surviving objects in Eden and Survivor are copied to another Survivor space at once, and Eden and the Survivor space that was just used are cleaned up. The default size ratio of HotSpot VIRTUAL machine Eden to Survivor is 8:1. When Survivor space is insufficient, other memory (in this case, older years) is required for allocation and guarantee (Handle Promotion).
3. Mark-tidy algorithm
The copy-collection algorithm has to carry out more replication operations when the object survival rate is high, and the efficiency will be low. More importantly, if you do not want to waste 50% of the space, you need to have extra space for allocation guarantee, in the extreme case that all objects in the memory used are 100% alive, so in the old days, this algorithm generally cannot be used directly.
The mark-compact algorithm then emerged, in which the marking process remained the same as the mark-clean algorithm, but instead of cleaning up the recyclable objects directly, the next step was to move all surviving objects to one end and then clean up memory directly beyond the end boundary.
FIG. 1.3 Schematic diagram of mark-collation algorithm
4. Generational collection algorithm
Current garbage Collection in commercial virtual machines is done using “Generational Collection,” an algorithm that doesn’t have any new ideas and just divides memory into chunks based on the lifetime of the object. Generally, the Java heap is divided into the new generation and the old generation: in the new generation, a large number of objects are found dead and only a small number of objects are found alive during each garbage collection, so the replication algorithm is used, and only a small amount of the replication cost of the living objects can be collected. In the old age, because the object has a high survival rate, there is no extra space for it to allocate guarantee, so it must use “mark-clean” or “mark-tidy” algorithm to recycle.
Memory allocation and reclamation policies
The automatic memory management advocated in the Java technology architecture ultimately boils down to solving two problems automatically: allocating memory to objects and reclaiming memory allocated to objects.
In general, objects are allocated on the heap (but can also be JIT compiled to split into scalar types and allocated on the stack indirectly). Objects are allocated on the Eden region of the new generation. If local thread allocation buffers are enabled, TLAB(Thread Local Allocation Buffer) will be allocated first by Thread. In the old days, allocation rules were not 100 percent fixed, depending on which combination of garbage collectors was being used and the memory-related parameter Settings in the virtual machine.
1. Objects are allocated in Eden area first
In most cases, objects are allocated in the Eden region of the new generation. When the Eden area does not have enough space to allocate, the virtual machine will initiate a Minor GC.
Note: Is there any difference between the Minor AND Full GC? - Minor GC: Refers to garbage collection in the new generation. Since Java objects tend to live and die, Minor GC is very frequent and usually fast. - Major GC (Full GC) : GC that occurs in an older era, with Major GC occurring, often accompanied by at least one Minor GC (but not always). Major GC is typically 10 times slower than Minor GC.Copy the code
2. Big objects go straight to the old age
Large objects are Java objects that require a large amount of contiguous memory. The most typical large objects are long strings and arrays. Large object for the virtual machine’s memory allocation is a bad news, than to have a large object is more bad news came across a group of “the evening out” “short-lived objects”), often appear large objects can easily lead to memory space when there are many triggers garbage collection in advance in order to get enough contiguous space to “put” them.
3. Long-lived objects will enter the old age
To manage objects in generations, virtual machines define an Age counter for each object. If the object is still alive after Eden is born and after a Minor GC and can be accommodated by Survivor, it is moved to Survivor space and the object age is set to 1. Each time an object “survives” a Minor GC in a Survivor zone, its age increases by one year, and when it reaches a certain age (15 by default), it will be promoted to the old age. The threshold for the object to be promoted to the old age can be set by using -xx :MaxTenuringThreshold.
4. Dynamic object age determination
In order to better adapt to the memory conditions of different programs, virtual machines do not always require that the age of objects must reach MaxTenuringThreshold to advance to the old age. If the sum of all objects of the same age in the Survivor space is greater than half of the size of the Survivor space, Objects older than or equal to this age can go directly to the old age without waiting until the age specified in MaxTenuringThreshold.
conclusion
This note provides a brief overview of some of the garbage collection algorithms and memory allocation strategies we’ve talked about so far, so that we can understand the process of automatic memory management for the JVM, namely how to allocate and reclaim memory allocated to objects.