“This is the first day of my participation in the Gwen Challenge in November. See details of the event: The last Gwen Challenge in 2021”.

JVM memory partition

Garbage collector

Garbage collection algorithm

Reference counting algorithm

Principle: Adding a reference counter to an object Defect: does not resolve cyclic dependencies Benefit: The reference counting algorithm is beneficial. Garbage collection can be performed in real time without having to start a separate thread for reacability analysis. Actual usage scenario: The Java VIRTUAL machine is not in use, but Python is in use

Accessibility analysis algorithm

How it works: Through a series of ‘GC Roots’ objects as starting points, the path from these nodes is called the reference chain. An object is unavailable when there is no reference link to GC Roots.

Available as objects for GC Roots:

  • The object referenced in the virtual machine stack (the local variable table in the stack frame)
  • The object referenced by the class static property in the method area
  • The object referenced by the constant in the method area
  • Objects referenced by JNI(commonly referred to as Native methods) in the Native method stack

Mark-clear

Mark clear directly can.Copy the code

Inefficiency: inefficient, space will produce a lot of debris

Mark-copy

Divide the space into two pieces and GC only one at a time. When a block of memory is used up, the surviving objects are copied onto another block.Copy the code

Solve the shortcomings of the former method, but it will cause low space utilization. Because most new generation objects don’t survive the first GC. So there’s no need to divide the space 1 to 1. You can split a larger Eden space and two smaller Survivor Spaces, using the Eden space and one Survivor at a time. When recycling is done, the surviving objects in Eden and Survivor are copied to another Survivor at a time, and the Eden and Survivor space is cleaned up. The size ratio is generally 8:1:1, wasting 10% of Survivor space each time. But the question here is what if more than 10% survive? An allocation guarantee strategy is used here: the extra objects go straight into the old age.

Mark-tidy

The memory defragmentation algorithm is also added on the basis of marker clearingCopy the code

Generational recycling

Generation collection theory is based on three hypotheses and rules of thumb.

  • Weak generation hypothesis

The vast majority of objects are collected on the first garbage collection, and as a rule of thumb, this value is as high as 98 percent.

  • Strong generation hypothesis

Objects that survive more collections are less likely to die.

  • Cross-generational citation hypothesis

This hypothesis assumes that there will be very few cross-generational references. Because after a few times of garbage collection, even if there are cross-generation references, the new generation will become the old generation, and the cross-generation references will naturally disappear, so the number of cross-generation references will not be much. When collecting new generation objects, you need to find them because there may be older objects that reference them. According to the cross-generation reference hypothesis, the number of these cross-generation references will not be too large. Compared with scanning the old age, a global data structure (memory set) will be established in the new generation to record which piece of old age memory will have cross-generation references, although maintaining this data structure also requires a small amount of overhead. But it still looks like a better deal.

According to generational theory, objects in the new generation have a high probability of being collected in one garbage collection, while objects in the old generation have a low probability of being collected in the next garbage collection. Because of this difference, the new generation and the old generation garbage collection use different garbage collection algorithms and garbage collectors.

A large number of objects die and only a few survive each garbage collection in the new generation, so it is reasonable to choose the replication algorithm. In the old age, the object has a higher survival rate and no additional space allocation guarantees it. So you must use a tag sweep or tag defragment algorithm for recycling.

Extended learning
What is a memory set?

A memory set is a data structure for recording a collection of Pointers from a non-collection region to a collection region.

If we disregard efficiency and cost, we can store all the old objects with Pointers to the new generation in an array. But if this is the case, our maintenance costs are very good. For example, if all the old age objects have Pointers to the new generation, then we need to maintain the entire old age size memory set, which is definitely not desirable. So we introduced the data structure of card table

What is a card table?

Memory set is an idea we put forward for cross-generation reference problem, and card table is a concrete realization of this idea. Memory set is structure, card table is implementation class.

In the hotspot VIRTUAL machine, the card table is an array of bytes. Each entry in the array corresponds to a contiguous address region in memory. If there is a reference to an object in the region to be reclaimed, the element in the card table array is set to 1; if there is no reference to an object in the region to be reclaimed, the element is set to 0. Below for a card table, a block of memory address to the right of nine (512), equivalent to divided by positioning to a card table elements, that is to say, every 512 bytes of memory in continuous area area, with a card table to locate if the card table corresponding element 1 represents the 512 bytes have a pointer to the region.

This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.