A Complete Guide to GC in Java Garbage Collection Mechanism, Let You Thoroughly Understand the Operating Principle of JVM

1. GC process

1) First judge whether the object is alive (whether it is garbage)

It can be judged by the reference counting algorithm and the reachability analysis algorithm. Since the reference counting algorithm cannot solve the problem of circular references, the reachability analysis algorithm is currently used

2) Traverse and recycle objects (recycle garbage)

Garbage can be reclaimed by the garbage collector (Serial/Parallel/CMS/G1), and the algorithms used by the garbage collector are mark clearing algorithm, mark finishing algorithm, copy recovery algorithm and generational recovery algorithm.

2. Types of GC

3. GC collection method

Marking and clearing : mark first, then clear after marking, the efficiency is not high, and debris will be generated

Mark finishing : After marking, let all surviving objects move to one end

Copy recycling : The ratio of S0 and S1 in Eden area is 8:1:1, that is, the YGC mentioned above uses the copy recycling algorithm.

4. GC collector

4.1 Several collectors

1) Serial collector

A single-threaded collector, when performing garbage collection, must suspend all other worker threads until it finishes collecting.

Features: The CPU utilization rate is the highest, and the pause time means that the user waits for a long time.

Applicable scene: small application

The serial garbage collector can be used through the JVM parameter -XX:+UseSerialGC.

2) Parallel collector (jdk8 default collector)

Use multiple threads to scan and compact the heap

Features: short pause time, high recovery efficiency, high throughput requirements.

Applicable scenarios: large-scale applications, scientific computing, large-scale data collection, etc.

Turn on the concurrent mark sweep garbage collector via the JVM parameter XX:+USeParNewGC.

3) CMS collector

Implemented by the "mark-clear" algorithm, using a multi-threaded algorithm to scan the heap and reclaim unused objects.

(1) Initial mark

(2) Concurrent marking

(3) Concurrent preprocessing

(4) Relabeling

(5) Concurrent clearing

(6) Concurrent reset

Features: Response time priority, reducing garbage collection pause time

Applicable scenarios: servers, telecommunications fields, etc.

Set by JVM parameter -XX:+UseConcMarkSweepGC

4) G1 collector (jdk17 default collector)

In G1, the heap is divided into a number of contiguous regions. The G1 algorithm is used for recycling, which absorbs the characteristics of the CMS collector.

Features: support large heaps, high throughput

--Support multiple CPU and garbage collection threads

--Use parallel collection while the main thread is paused

--Use concurrent collection while the main thread is running

Real-time goal: configurable to take up to M milliseconds for garbage collection within N milliseconds

Use the G1 garbage collector through the JVM parameter –XX:+UseG1GC

4.2 Comparison of Several Collectors

5. Summary of GC

Relationships between concepts throughout garbage collection

  • The core of the GC collector is the GC collection algorithm
  • The GC collection algorithm generally first needs to determine whether the object is alive, and then uses the reference counting algorithm or the reachability analysis algorithm
  • The reference counting algorithm cannot solve the situation of circular references, so the reachability analysis algorithm is currently used
  • GC is divided into 4 types, acting on different areas of memory (new generation Eden/S0/S1, old generation). At this time, the GC collectors will combine with each other to complete different types of GC, so as to achieve the function of JVM GC

6. Related issues

Java GC is an automatic memory management mechanism for detecting and cleaning up objects that are no longer used. Its main function is to release memory space for continued use by the program.

2. What are the different types of GC algorithms in Java?

There are several different types of GC algorithms in Java, including mark-sweep, copy, mark-compact, generational, and incremental garbage collectors, among others.

3. Please explain what is Minor GC and Major GC, and the difference between them.

Minor GC is used to clear the surviving objects of the new generation, while Major GC is used to clear the surviving objects of the old generation. Minor GCs are usually more frequent and faster than Major GCs.

4. What is a heap? How is the heap managed in Java?

The heap is one of the Java runtime data areas used to store object instances. In Java, you can manage the heap by setting the -Xmx and -Xms parameters to control the size of the heap.

5. What is Garbage Collector? What are the different types of garbage collectors in Java?

The garbage collector is responsible for performing garbage collection operations. In Java, there are several different types of garbage collectors, including serial collectors, parallel collectors, CMS collectors, and G1 collectors, among others.

6. Describe the garbage collection process. What happens when the garbage collector runs?

During garbage collection, the garbage collector scans the objects in the heap memory and marks all objects that are no longer used. It then removes these objects from the heap to free memory space.

7. Why do you need to avoid frequent garbage collection?

Frequent garbage collection will lead to poor program performance, because the garbage collector needs to occupy CPU time and memory resources. Additionally, frequent garbage collections can cause delays or stalls in the application.

8. How to manually trigger GC using Java code?

In Java, GC can be manually triggered using the gc() method of the System class. But it should be noted that calling this method does not guarantee that garbage collection will occur immediately, because Java's garbage collection mechanism is automatically managed by the JVM.

9. Describe the generational garbage collection algorithm. Why use generational garbage collection in Java?

The generational garbage collection algorithm divides the heap into the new generation and the old generation, and selects different garbage collection methods according to the characteristics of the objects. Using generational garbage collection in Java can improve the efficiency of garbage collection, because most objects are short-lived and only exist in the new generation.

10. How to optimize GC performance? What do I need to pay attention to when making adjustments?

You can optimize GC performance by adjusting the heap size, setting the garbage collector, and adjusting the parameters of the garbage collector. When adjusting, you need to pay attention not to over-adjust, otherwise it will cause performance degradation or abnormal application.


Finally, I recommend the drawing tools in the article:

​​​​​​​

​​​​​​​

ProcessOnhttps://www.processon.com/i/5b100cd0e4b06350d44a08ac?full_name=%E7%8E%84%E6%98%8EHanko

Guess you like

Origin blog.csdn.net/citywu123/article/details/130114512