JVM! Garbage collection mechanism!

GC heap, heap partition, JVM memory allocation and recovery

1. Basic structure of heap space

Cenozoic generation: Eden space From Survivor To Survivor space Old generation immortal generation (meta space is direct memory)

        

        In most cases, objects will be allocated in the Eden area first (a Minor GC will be performed when the space in the Eden area is insufficient and the threshold is reached), and at the same time, large objects will directly enter the old generation, and long-lived objects will directly enter the old generation (old generation When the space is not enough, Old GC will be performed ; when the old generation space is full, Full GC will be performed directly )

        

Young generation garbage collection mechanism (Minor GC)

detailed steps:

1. New object creation: When a Java program creates a new object, it will be allocated to the Eden area

2. Eden area is full: When the number of objects in the Eden area reaches the set threshold, it will be considered as full

3. Trigger MinorGC: When the Eden area is full, trigger the mechanism, this process suspends all application threads, and scans the surviving objects in the young generation according to the garbage collection algorithm at the same time

4. Copying of surviving objects: After finding surviving objects, copy them from the Eden area and the Survivor area to another empty Survivor area, and then perform a sort according to age, among which the objects whose age is greater than or equal to the threshold are placed in the old generation

5. Empty the Eden area and the Survivor area used last time: After garbage collection, the Eden area and the Survivor area used last time will be cleared

6. The From area and the To area are reversed: Before the next Minor GC, the S0 area (usually the From area) and the S1 area (usually the To area) will exchange positions. At this time, the original S0 area becomes the new To area, and the original S1 area becomes the new From area.

The main steps:

1. Copying of surviving objects: When performing garbage collection of the young generation, the Java virtual machine first finds all surviving objects and copies them from the Eden area and the Survivor area to another empty Survivor area. At the same time, in this process, the Java virtual machine sorts objects according to their age. Normally, the age of the object will increase by 1 every time Minor GC passes through; in order to prevent the copied object from occupying all the space in the Survivor area, the Java virtual machine will also put objects whose age is greater than or equal to the threshold into the old generation;

2. Empty the Eden area and the Survivor area used last time: After the surviving objects are copied, the Java virtual machine clears the Eden area and the Survivor area used last time. In this way, these areas can be reused

3. Inversion of From area and To area: In order to ensure that the two Survivor areas can be fully utilized, each time Minor GC is performed, the Java virtual machine will swap the From area and To area. Therefore, before the next Minor GC, the original S0 area (usually the From area) becomes the new S1 area, and the original S1 area (usually the To area) becomes the new S0 area;

Personal understanding:

You can compare the young generation garbage collection mechanism to a delivery company.

First of all, this express company has a large warehouse, which is equivalent to the heap memory of the Java virtual machine. In this warehouse, there are three areas for storing goods: Eden area, S0 area and S1 area. Among them, the Eden area can be regarded as the delivery area of ​​the express company, while the S0 area and S1 area are the two transfer stations of the express company;

1. When a new shipment is sent out, it will be put into the delivery area (ie Eden area). If the delivery area is full, the courier company will carry out a "clearance" operation to repack all the goods that have not yet reached the destination, and then send them to the transfer station (that is, S0 area or S1 area)

2. At the transfer station, the courier company will screen out all the goods that have not yet reached their destination, repack them, and send them to another empty transfer station again. This process is equivalent to the copying process of surviving objects in the young generation

3. At the same time, the courier company will also arrange different transportation methods according to the importance and timeliness of the goods. For goods with small size, light weight, and high timeliness requirements, the express company will choose air transportation; for goods with large size, heavy weight, and low timeliness requirements, it will choose land transportation. This process is analogous Surviving objects in the young generation are moved to the old generation

4. Finally, in order to ensure that the transfer station can be fully utilized, the express company will regularly exchange the two transfer stations. In this way, each transfer station has sufficient time to process all the goods. This process is equivalent to the reversal of the From area and the To area.

Allocation Guarantee Mechanism:

Trigger mechanism: used to ensure that when Minor GC occurs, all surviving objects in the new generation can still be transferred to the old generation, thereby avoiding OutOfMemoryError exceptions caused by memory allocation failures

When Minor GC occurs, if the Eden area and the Survivor area cannot accommodate all surviving objects, then the virtual machine will directly promote these objects ( all surviving objects ) to the old age. In order to ensure that these objects can enter the old age smoothly, an allocation guarantee mechanism needs to be used at this time

Basic idea: Before performing Minor GC, first check whether the remaining space in the old generation is enough to accommodate the objects to be promoted. If it is enough, perform Minor GC directly; otherwise, perform a Full GC to release the old generation that is no longer used objects, and move all surviving objects to the old generation;

Mark-Copy Algorithm: Divide the memory into two pieces of the same size, and use one of them each time. When this block of memory is used up, copy the surviving object to another block, and then clean up the used space at one time. In this way, each memory recovery is to reclaim half of the memory range;

Old Generation Garbage Collection Mechanism

Several situations when the young generation enters the old generation

1. The age of the subject exceeds the threshold

2. Large objects directly enter the old generation

3. Long-lived objects enter the old age

detailed steps:

1. Initial mark (Initial Mark): At this stage, all objects in the old generation will be traversed and all directly referenced objects will be marked. This process requires stopping the execution of the application

2. Concurrent Mark: At this stage, the GC thread will mark all surviving objects in the old age concurrently with the application thread. This process does not require stopping the execution of the application.

3. Remark (Remark): After the concurrent marking is completed, it is necessary to traverse all the objects in the old generation again in order to find out the objects that have changed during the concurrent marking and mark them. This process requires stopping the execution of the application.

4. Concurrent Sweep: At this stage, the GC thread will clean up useless objects in the old generation concurrently with the application thread, and release their memory to the operating system. This process does not require stopping the execution of the application.

5. Reset space (Compact): After the concurrent clearing is completed, there may be a large amount of discontinuous memory space in the old generation. In order to reduce memory fragmentation, surviving objects need to be compressed together to free up more contiguous space for the application to use.

Mark-sweep algorithm: In the mark-sweep algorithm, the garbage collector traverses all objects in the old generation and marks all live objects. Then, all objects that are not marked will be considered garbage objects and need to be cleared

Disadvantages: This process will cause a lot of memory fragmentation, because the space occupied by the cleared objects is not necessarily continuous, leaving many scattered small pieces of space. If the size of the new object exceeds the size of any of these small blocks, then it cannot be allocated in these small blocks, causing memory allocation to fail

Mark-collation algorithm: In order to avoid memory fragmentation problems, you can use the mark-compact algorithm for garbage collection in the old age. In this algorithm, the garbage collector will traverse all objects in the old generation and mark all live objects. It then moves all live objects to one end so that the space they occupy is contiguous

When all active objects have been moved to one end, the garbage collector will clear all unmarked objects in the old generation and free up all the remaining space. In this way, the problem of memory fragmentation can be avoided and memory utilization can be improved.

How to judge the object has died?

1. Reference counter

Add a reference counter to the object, every place references it +1, reference invalidation -1, if it is 0, it is dead

2. Accessibility Analysis Algorithm

A series of objects called "GC Roots" are used as the starting point to search downward from these nodes. The path that the node traverses is called a reference chain. When an object is not connected to GC Roots by any reference chain, proves that the object is unavailable

How to tell if a constant is obsolete? How to judge a class is useless class?

1. If the string "abc" exists in the constant pool , if there is no String object currently referencing the string constant, it means that the constant "abc" is an obsolete constant. If memory recovery occurs at this time and it is necessary Then, "abc" will be cleared out of the constant pool by the system

2. All instances of this class have been recycled, that is, there are no instances of this class in the Java heap. The ClassLoader that loaded this class has been recycled. The java.lang.Class object corresponding to this class is not referenced anywhere, and the methods of this class cannot be accessed through reflection anywhere.

What is the parental delegation model? effect?

        The parent delegation mode is a working method of ClassLoader in Java, which provides a security mechanism to avoid repeated loading of Java class libraries and ensure the stability of Java programs:

1. When a Java class needs to be loaded, the ClassLoader of the current thread will first try to load the class

2. If the custom ClassLoader cannot find the class, it will delegate the request to the parent ClassLoader for processing

3. When the parent ClassLoader cannot load this class, it will continue to delegate to a higher-level ClassLoader until the Bootstrap ClassLoader

4. If the Bootstrap ClassLoader cannot load the class, it will return to the original sub-ClassLoader, and then the sub-ClassLoader will throw a ClassNotFoundException

In the parental delegation mode, each ClassLoader has a parent ClassLoader. To load a certain class, ClassLoader will first ask whether its parent ClassLoader has already loaded the class, if not, ClassLoader will load the class by itself. In this way, the concept of shared classes can be implemented in the entire JVM, avoiding the situation that the same class is loaded repeatedly, and at the same time ensuring the stability and security of the program

common garbage collector

1.Serial collector 

advantage:

1. Low latency: Due to the use of single-threaded garbage collection, the overhead of context switching caused by multi - threaded concurrency can be avoided, thereby achieving low latency

2. Simple and efficient: Due to its simple implementation, it does not require too many system resources and can quickly recycle garbage objects

shortcoming:

1. Poor performance in high-concurrency scenarios: Since garbage collection is performed by a single thread, there may be long pauses in high-concurrency scenarios, which affects system throughput

2. Large memory usage: Since the entire application process needs to be monopolized during garbage collection, when the garbage collection takes a long time, the memory usage of the system may be large

Scenes:

  1. small web application
  2. mobile device application
  3. Test environment during development phase

algorithm:

  1. Marking phase: traverse the object graph from the root node, and mark all referenced objects.
  2. Cleanup phase: traverse all objects in the heap memory and recycle unmarked objects

2.Serial Old collector

advantage:

  1. The implementation is simple, the amount of code is small, so the operation efficiency is high;
  2. Low memory consumption, suitable for environments with small memory;
  3. Can be used together with the Serial collector to become a continuous garbage collector combination;
  4. For smaller applications or applications with a small amount of data, Serial Old can show good performance

shortcoming:

1. The advantages of multi-core CPU cannot be fully utilized due to single-threading. When a large amount of data needs to be processed, it may cause the STW time to be too long;

2. The mark-sorting algorithm requires additional overhead when dealing with large objects, because large objects need to be moved, which will also affect the efficiency of the garbage collector

Scenes:

  1. Applications in a single-core CPU environment;
  2. Small applications, because the working set is small and the amount of data is not large;
  3. For applications that require high startup speed

algorithm:

        Mark-sorting algorithm, which can ensure that the space after recovery is continuous, reducing the generation of fragments. However, this algorithm needs to copy the surviving objects to another segment of memory, and needs to mark all surviving objects, so when processing a large amount of data, it will cause the STW time to be too long

3. ParNew collector

advantage:

  1. Multi-thread parallel processing: Using multiple threads to perform garbage collection at the same time can make full use of the performance of multi-core CPUs and improve the efficiency of garbage collection.

  2. Low latency: The ParNew collector adopts the mark-copy algorithm to divide the heap memory into the young generation and the old generation, and only reclaims the young generation. Since the young generation occupies less heap memory, the garbage collection speed is faster, which can ensure low latency.

  3. Used in conjunction with the CMS collector: The ParNew collector is usually used in conjunction with the CMS collector for parallel collection of the young generation, and the CMS collector is responsible for the concurrent collection of the old generation, which can ensure the efficiency and low latency of the entire garbage collection process

shortcoming:

  1. Only applicable to young generation garbage collection: The ParNew collector can only collect garbage on the young generation, not the old generation.

  2. Single-threaded execution of STW operations: When performing young generation garbage collection, all threads of the application (STW) need to be suspended. This operation can only be completed by one thread, which may cause a long pause time

Scenes:

        Scenarios with low latency and high throughput

algorithm:

        mark-copy algorithm

4. CMS collector

advantage:

  1. Low pause time: The CMS collector adopts the mark-clear algorithm. When performing garbage collection, it does not need to suspend all threads (STW) of the application. Garbage collection can be performed while the application is running, thereby ensuring low pause time.

  2. High throughput: Since the CMS collector adopts a concurrent collection algorithm, multiple threads can be used for garbage collection while the application is running, thereby ensuring high throughput.

  3. Fully self-adaptive: The CMS collector can dynamically adjust the garbage collection strategy according to the running conditions of the application to ensure the best garbage collection effect and performance

shortcoming:

  1. Memory fragmentation problem: Since the CMS collector uses the mark-clear algorithm, it will cause a large amount of memory fragmentation in the heap memory, which may affect the performance of the application.

  2. Overhead caused by concurrent execution: When the CMS collector executes garbage collection concurrently, it needs to consume a certain amount of CPU resources and memory space, which may have a certain impact on the performance of the application

Scenes:

        Suitable for scenarios requiring low pause time and high throughput, usually used with the ParNew collector

algorithm:

        mark-sweep algorithm

5. Parallel Scavenge collector

advantage:

  1. High throughput: The Parallel Scavenge collector can utilize multiple CPU cores to execute garbage collection tasks in parallel to ensure high throughput.

  2. Short STW time: The Parallel Scavenge collector uses mark-copy algorithm and generational garbage collection strategy, which can reduce STW time to a certain extent.

  3. Adaptive adjustment: The Parallel Scavenge collector can automatically adjust the garbage collection strategy according to the current system load to achieve the best garbage collection effect and performance

shortcoming:

  1. Memory space usage problem: The Parallel Scavenge collector adopts the mark-copy algorithm and needs to allocate two memory spaces of the same size to save objects, so the problem of insufficient memory may occur in applications with small heap space.

  2. Not suitable for long-running applications: Since the Parallel Scavenge collector is mainly used for garbage collection of the new generation, it is not suitable for long-running applications

Scenes:

  1. The application has high throughput requirements: When the Parallel Scavenge collector performs garbage collection, it will use as many CPU cores as possible and reduce the STW time to improve the throughput of the application.

  2. Applications have low response time requirements: Although the Parallel Scavenge collector is not specifically designed to reduce STW time, it can reduce STW time to a certain extent due to the use of mark-copy algorithm and generational garbage collection strategy

algorithm:

        mark-copy algorithm

6. Parallel Old Collector

advantage:

  1. High throughput: The Parallel Old collector can utilize multiple CPU cores to execute garbage collection tasks in parallel to ensure high throughput.

  2. Shorter STW time: The Parallel Old collector uses a mark-sort algorithm, which can organize memory space during garbage collection, thereby reducing STW time.

  3. Adaptive adjustment: The Parallel Old collector can automatically adjust the garbage collection strategy according to the current system load to achieve the best garbage collection effect and performance

shortcoming:

  1. Memory space usage problem: Since the Parallel Old collector uses a mark-sort algorithm, it needs to sort out the entire heap space when performing garbage collection, so a large memory space is required.

  2. Not suitable for long-running applications: Since the Parallel Old collector is mainly used for garbage collection in the old generation, it is not suitable for long-running applications

Scenes:

  1. Applications have high throughput requirements: By executing garbage collection tasks in parallel, the Parallel Old collector can utilize multiple CPU cores as much as possible and reduce STW time to improve application throughput.

  2. The application has a large heap space requirement: because the Parallel Old collector adopts the mark-collation algorithm, it can organize the memory space during the garbage collection process, thereby reducing memory fragmentation, and is suitable for applications that require large-capacity heap space

algorithm:

        Mark-Collating Algorithm

7. G1 collector

advantage:

  1. Predictable pause time: G1 collector adopts generational garbage collection strategy and concurrent garbage collection algorithm, which can reduce STW time to a certain extent and guarantee predictable pause time.

  2. High throughput: The G1 collector can perform multiple small garbage collection tasks at the same time, thereby ensuring high throughput.

  3. High efficiency of memory space usage: Since the G1 collector adopts mark-sorting algorithm and generational garbage collection strategy, it can reduce the waste of memory space to a certain extent and improve the efficiency of memory space usage

shortcoming:

  1. Compared with other collectors, the G1 collector consumes more CPU resources and memory space.

  2. In scenarios dealing with a large number of short-lived objects, the G1 collector may not perform as well as concurrent collectors such as CMS

Scenes:

  1. Applications with large memory requirements: Since the G1 collector can garbage collect and organize the entire heap space, it is suitable for applications that require large heap spaces.

  2. Applications have low latency requirements: G1 collector adopts concurrent garbage collection algorithm and generational garbage collection strategy, which can reduce STW time to a certain extent and ensure low latency

algorithm:

        Mark-Collating Algorithm

Guess you like

Origin blog.csdn.net/weixin_64625868/article/details/131026573