The interviewer, the charter woman, asked me: What garbage collectors does the JVM have? What are their characteristics?
First, garbage collection algorithms
1. Mark clearing algorithm
-
1. Main process:
- A. Mark: Sweep down GC Roots first. Cleaning starts from 101 to inspect all rooms and mark a sign at the door: Is this room ready for cleaning? Has the current room air conditioner remote control borrowed from another room been returned, i.e. is the application held by another object?
- B. Clear: Clean the rooms marked as clean one by one.
-
2. Disadvantages:
- Two scans, low efficiency and existence of space debris, clean out the room is not continuous
-
3. Suitable scene:
- Old age: Old age has more living objects, fewer to mark, and fewer to clear.
2. Replication algorithm
-
1. Main process: Copy available objects to a new space. Leave the garbage in one area so that I can clean up the garbage in this area.
-
2. Disadvantages:
- Waste of space
- Increase the cost of adjusting references: The reference of an object needs to be adjusted when it moves to a new space. Usage scenario: Eden area, most objects live and die.
3. Mark sorting algorithm
-
1. Main process: Sweep down the GC Roots to deal with garbage, and then arrange available objects in space to ensure continuity.
-
2. Usage scenario: As mentioned above, the space debris is marked to be cleared, so can the cleaning service “invite” the tenants in the room that needs to be cleaned to another clean room when cleaning?
- Old age: Old age has more living objects, fewer to mark, and fewer to clear.
-
3. Disadvantages: Not only mark all live objects, but also sort out the reference addresses of live objects. More work per unit time leads to lower natural efficiency.
Object memory allocation process
New -> Try stack allocation -> Try thread local allocation -> Enter Eden zone -> Survive zone 15 times by default -> Enter old age
- Large objects go directly to the old section.
- Dynamic age determination: when copying objects from S0 to S1 and finding that S1 has more than half of the memory, the oldest age will be placed in the old age.
- Allocate guarantees: In YGC, a large object comes in but there is not enough space in survivor to guarantee the old age
Three: view the current JVM Settings
- XX: + PrintFlagsFinal - version # pipeline filter Java - XX: + PrintFlagsFinal - version | grep NewRatio optionCopy the code
Garbage collector
- Serial Serial Serial Serial Serial Serial Serial Serial Serial Serial Serial Serial Serial Serial Serial The algorithm is: the new generation of replication algorithm
- The Parallel Exploiter (PS) : a young generation of Parallel recycler.
- ParNew: multithreaded version of Serial for the younger generation. Cooperate with CMS for parallel collection
- SerialOld: old single-thread collector, algorithm: tag collation algorithm
- ParallelOld: A parallel collector from an old age.
- ConcurrentMarkSweep (CMS), concurrent token sweep, enables business threads to run while the garbage collector is working.
- G1 heap space is divided into several small regions for logical generation instead of physical generation.
G1 as bellow:
The rental woman asked me: Do you know anything about G1 recyclers? What is the tricolor mark?
The renter asked me again: How can I achieve the maximum rent return with G1 recycler?
-
CMS collector basic execution process:
1. Initial tag: STW but fast, only tag all root objects.
2. Concurrent tagging: Go down the root object and the worker thread can continue to run while tagging and the tenant can continue to garbage.
3. Final mark: STW. When the concurrent mark is made, the working thread is still running.
4. Concurrent removal: The marked garbage will be removed, but there is floating garbage. After the marking is completed, the tenants can continue to play, and the garbage generated during this period is floating garbage.
CMS faults:
-
Floating garbage
Optimization: Lower the threshold for triggering the CMS and keep the old age free. When the old age reaches 70%, CMS garbage collection is triggered.
-XX:+CMSInitiatingOccupancyFraction=70
Copy the code
CMSInitiatingOccupancyFraction the default value is 1, if CMSInitiatingOccupancyFraction between 0 ~ 100, ((100 – MinHeapFreeRatio) + (double)(CMSTriggerRatio * MinHeapFreeRatio) / 100.0) / 100.0
-
Space debris: causes objects to come and cannot be allocated in the old age.
Optimization: Enable the tag collation algorithm
+ UseCMSCompactAtFullCollection - - XX: XX: CMSFullGCsBeforeCompaction # default to 0 refers to how many times before FGC is compressedCopy the code
If this article helps you, give it a thumbs up. Let me know you came by