Garbage Collection-GC20+ asks to help you thoroughly understand GC

Write in front:

Recently, I have sorted out the basic knowledge and clarified the ideas. At the same time, I will cooperate with the actual interview or increase or decrease the hypothetical questions about GC to help me sort out the knowledge points of this module. I hope to help those in need friend.

 

What is GC?

Java language-specific garbage collection mechanism

What can GC do?

As a garbage collection mechanism, its role is obviously to collect garbage objects. It mainly does three things:

1. Determine which objects need to be recycled

2. When to recycle garbage objects

3. How to recycle garbage objects

So how does GC determine which objects need to be recycled?

There are two common methods: reference counting method and reachability analysis method

In addition to these two methods, do you know other methods?

Escape analysis

Talk about escape analysis?

The principle of escape analysis: It is to analyze the dynamic scope of the object to determine whether the object escapes the original area due to external references

example:

A method is passed to other methods in the form of call parameters, which is called method escape

If it can be proved that an object will not escape to the original method or the original thread, that is, other methods cannot access the object in any way, then we can perform some efficient optimizations on the object, such as allocation on the stack. The memory space occupied by the object can be destroyed as the stack frame is popped. If a large number of non-escape objects can be analyzed, the memory occupied by the stack frame will be released as soon as the stack frame is popped out, which will obviously share the recovery pressure of GC

Do you understand the GC Roots object? Talk about accessibility analysis

Reachability analysis is to use a certain off-heap reference object as the root node of the reference chain, and search downwards until the object to be analyzed is a reference chain. If the reference chain of an object is 0, that is, all off-heap reference objects cannot search for this object, then the object is unreachable, indicating that the object can be recycled by GC

GC Roots objects include: objects referenced by the local variable table in the stack frame in the JVM stack, objects referenced by the JNI in the local method stack, objects referenced by constants and static property variables in the method area

By the way, reference counting?

The object maintains a counter when it is initialized and assigned. Whenever the object is referenced elsewhere, the counter will do a +1 operation, indicating the current number of references to the object. If the value of the counter is 0 at a certain moment, then Indicates that the object can be recycled by GC

Do you understand the shortcomings of reference counting?

1. First of all, each object needs to maintain a counter. If there are more objects, the overhead used to maintain the counter will become larger, which is obviously unreasonable

2. The algorithm cannot handle the problem of circular references between objects

Where does GC work? When does it work?

GC works in the heap memory, mainly acting on the young generation (relatively frequent) in the heap memory, and can also act on the old generation (relatively less). GC will be triggered whenever the memory storage volume triggers the set threshold For recycling

What kind of GC do you know? Tell me where they all work and what do they do?

Minor GC: Acting on the young generation, it mainly uses the replication algorithm to recycle garbage objects, and the surviving objects are copied and retained, and enter the next space

Major GC: Acting in the old age, it mainly uses mark removal or mark sorting algorithms to clear dead objects

Full GC: Acting on the entire heap memory, it will be triggered when Minor GC is triggered multiple times and there is still not enough space available for allocation of the object to be created

 * [GC (Allocation Failure) [PSYoungGen: 1956K->501K(2560K)] 1956K->781K(9728K), 0.0024008 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [GC (Allocation Failure) [PSYoungGen: 2329K->504K(2560K)] 2609K->1327K(9728K), 0.0017602 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [GC (Allocation Failure) [PSYoungGen: 2217K->400K(2560K)] 4129K->2855K(9728K), 0.0008253 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [Full GC (Ergonomics) [PSYoungGen: 2154K->0K(2560K)] [ParOldGen: 6807K->2775K(7168K)] 8961K->2775K(9728K), [Metaspace: 3218K->3218K(1056768K)], 0.0066449 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
 * [GC (Allocation Failure) [PSYoungGen: 87K->32K(2560K)] 5038K->4983K(9728K), 0.0004724 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [GC (Allocation Failure) [PSYoungGen: 32K->32K(2560K)] 4983K->4983K(9728K), 0.0004280 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [Full GC (Allocation Failure) [PSYoungGen: 32K->0K(2560K)] [ParOldGen: 4951K->3863K(7168K)] 4983K->3863K(9728K), [Metaspace: 3222K->3222K(1056768K)], 0.0053085 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
 * [GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] 3863K->3863K(8704K), 0.0004326 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] [ParOldGen: 3863K->3843K(7168K)] 3863K->3843K(8704K), [Metaspace: 3222K->3222K(1056768K)], 0.0089908 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]

You talked about the replication algorithm, talk about its principle and how it is implemented in JVM

The principle of the replication algorithm:

Divide the memory into two areas (whether it is equally divided or not, it is recommended not to divide equally), one is used to produce (new) objects, the other is temporarily vacant, and then the algorithm is used to determine whether it is alive (HotSpot JVM uses reachability Analysis) Copy objects that are still alive after GC to the waiting space

Implementation of the replication algorithm in JVM:

There are two replication algorithms. First, objects are produced in the Eden (8/10) area. After GC, the surviving objects are copied to Survivor (2/10), and then Survivor is divided into two areas From (1/10) And To (1/10), the To area and From here are random, whoever is empty is To, and the other is From

What is the difference between mark removal and mark sorting? Which one is recommended?

The main difference between the two is that the mark-sweeping algorithm will generate memory fragments and waste resources after clearing garbage objects due to the uneven distribution of live objects, while the mark-sweeping algorithm does not. It will sort the live objects in another memory area. One end, only the end with rubbish is cleared, it is usually recommended to use the tag sorting algorithm

Do you understand references? Which references do you know?

References are divided into four types: strong references, soft references, weak references, and phantom references

Where can soft references be used?

Can be used to cache and associate some useful but not necessary objects

Do you understand GC tuning? Tell me about its principle? Have you ever adjusted it, and how?

principle:

Check the GC execution status through tools or GC logs. The most important thing is to check the GC pause point and the duration of the GC. Through the analysis and judgment of these data, the targeted parameter tuning or configuration tuning is performed. After the adjustment, the tool or GC performance after log verification adjustment

I use the form of GC log to optimize the GC. Set the maximum heap memory and the initial heap to be equal within the allowable range (1/4). In order to test and analyze the GC, I designed an OOM scenario to perform the GC log. View and analyze

* 堆内存调优——测试发生OOM
 *
 * [GC (Allocation Failure) [PSYoungGen: 1956K->501K(2560K)] 1956K->781K(9728K), 0.0024008 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [GC (Allocation Failure) [PSYoungGen: 2329K->504K(2560K)] 2609K->1327K(9728K), 0.0017602 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [GC (Allocation Failure) [PSYoungGen: 2217K->400K(2560K)] 4129K->2855K(9728K), 0.0008253 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [Full GC (Ergonomics) [PSYoungGen: 2154K->0K(2560K)] [ParOldGen: 6807K->2775K(7168K)] 8961K->2775K(9728K), [Metaspace: 3218K->3218K(1056768K)], 0.0066449 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
 * [GC (Allocation Failure) [PSYoungGen: 87K->32K(2560K)] 5038K->4983K(9728K), 0.0004724 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [GC (Allocation Failure) [PSYoungGen: 32K->32K(2560K)] 4983K->4983K(9728K), 0.0004280 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [Full GC (Allocation Failure) [PSYoungGen: 32K->0K(2560K)] [ParOldGen: 4951K->3863K(7168K)] 4983K->3863K(9728K), [Metaspace: 3222K->3222K(1056768K)], 0.0053085 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
 * [GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] 3863K->3863K(8704K), 0.0004326 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] [ParOldGen: 3863K->3843K(7168K)] 3863K->3843K(8704K), [Metaspace: 3222K->3222K(1056768K)], 0.0089908 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
 * Heap
 *  PSYoungGen      total 1536K, used 90K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
 *   eden space 1024K, 8% used [0x00000000ffd00000,0x00000000ffd168a8,0x00000000ffe00000)
 *   from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
 *   to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 *  ParOldGen       total 7168K, used 3843K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
 *   object space 7168K, 53% used [0x00000000ff600000,0x00000000ff9c0dc8,0x00000000ffd00000)
 *  Metaspace       used 3274K, capacity 4500K, committed 4864K, reserved 1056768K
 *   class space    used 354K, capacity 388K, committed 512K, reserved 1048576K
 * Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
 * 	at java.util.Arrays.copyOf(Arrays.java:3332)
 * 	at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
 * 	at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:674)
 * 	at java.lang.StringBuilder.append(StringBuilder.java:208)
 * 	at com.xiaozhao.juc.JVMDemo.main(JVMDemo.java:36)
 *
 * Process finished with exit code 1

Ever heard of the garbage collector? Can you say one or two of their realizations?

CMS、G1、Serial GC、ParNew GC、Parrallel GC。。。

Let's start with so much. The sorting and learning of Java knowledge has been continuing, and it has been a rookie, but it has been persistent. I hope that friends who see it can learn together. If there are deficiencies, please correct me!

Guess you like

Origin blog.csdn.net/weixin_43562937/article/details/107287382