Home > Java > javaTutorial > body text

Detailed explanation of the marking of recycled objects in Java and the secondary marking process of objects

黄舟
Release: 2017-10-11 10:12:43
Original
1762 people have browsed it

This article mainly introduces the relevant content of the marking of Java recycling objects and the secondary marking process of objects. The editor thinks it is quite good. I will share it with you here. Friends who need it can refer to it.

1. Marking of objects

1. What is a mark? How to mark?

I believe everyone knows the first question. Marking is to mark some dead objects to facilitate the cleaning of the garbage collector. As for how to mark, there are generally two methods: reference counting and reachability analysis.

Reference counting is relatively simple to implement. It is to add a reference counter to the object. Whenever there is a reference to it, it will be incremented by 1. When the reference is invalid, it will be decremented by 1. When the counter is 0, it will be decremented. Marked for recycling. This kind of judgment is very efficient, but many mainstream virtual machines do not use this method, mainly because it is difficult to solve the problem of circular references between several objects. Although it is not used much, it is still worth learning!


public class Test {
private Object obj;
Public static void main(){
Test t1=new Test();
Test t2=new Test();
t1.obj=t2;
t2.obj=t1;
t1=null;
t2=null;
//如果对象在这行发生gc,那么t1和t2对象是否能被回收
System.gc();
}
}
Copy after login

The basic idea of ​​reachability analysis is: by using some objects called "GC Roots" as the starting point, start searching from these nodes, search Objects that have a direct or indirect reference relationship with the node are combined in the form of a chain to form a "relationship network", also called a reference chain. Finally, the garbage collector collects some objects that are not in this relationship network. As shown in the figure:

The object connected to the GC Roots object is an object that is still alive, and the die obj on the right has nothing to do with GCROOTS, so it will be marked as a recyclable object. Currently, mainstream commercial virtual machines use similar methods. So what objects can be used as "GC Roots"? In Java, there are four types of objects that can be used as reference objects in "GC Roots"

1: Stack frame (noun in Chapter 1). (In the stack)

2: Object referenced by static properties. (In the method area)

3: Object referenced by constant. (In the method area)

4: Object referenced by JNI in the local method stack. (In the local method stack)

2. Secondary recycling of objects

I have said that the object is marked, but it does not mean that it is marked. Will it definitely be recycled? I don’t know if you guys remember that the Object class has a finalize() method. All classes inherit the Object class, so this method is implemented by default.

The working principle of finalize should be like this: once the garbage collector is ready to release the storage space occupied by the object, it first calls finalize(), and only during the next garbage collection process, will it really Reclaim the memory of the object. So if you use finalize(), you can perform some important cleaning or cleaning work during garbage collection.
When is finalize() called?

There are three situations

1. All objects are automatically called when they are Garbage Collection, such as when running System.gc().

2. The program exits The finalize method is called once for each object.

3. Explicitly call the finalize method

The purpose of this method is: before the object is recycled, the object's finalize() method will is called. The recycling here refers to after being marked. The problem lies here. Is there a situation where an object is no longer in the "relationship network" (reference chain) mentioned in the previous chapter, but when After the developer rewrote finalize(), and re-added the object to the "relationship network", that is to say, the object is still useful to us and should not be recycled, but it has been marked. What should we do?

In response to this problem, the virtual machine approach is to mark objects twice, that is, mark objects that are not in the "relationship network" for the first time. For the second time, it is necessary to first determine whether the object has implemented the finalize() method. If it has not been implemented, it will be directly determined that the object is recyclable; if it has been implemented, it will be placed in a queue first, and a low level created by the virtual machine. The priority thread will execute it, and then a second small-scale marking will be performed. This time the marked object will actually be recycled.

Summary: Simply put, the object is marked for the first time, and the object's finalize() method will be executed before the next GC. When executing the finalize() method, it is judged whether the object has implemented the finalize() method. If it is not implemented, it is cleared directly; if it is implemented, the object is placed in a queue to execute the finalize method and marked for the second time.
In java root search algorithm To determine the reachability of objects, unreachable objects do not necessarily have to be cleaned up. There is a probation period at this time. To truly judge that an object is dead, it must go through at least two marking processes: If the object finds that there is no reference chain associated with GC roots after performing a root search, it will be marked for the first time and marked. Once filtered, the filtering condition is whether it is necessary for this object to execute the finalize() method. When the object does not cover the finalize() method, or the finalize() method has been called by the virtual machine, the virtual machine will In all cases, it is regarded as "There is no need to execute ".

That is, when an object overrides the finalize() method, the object is determined to be necessary to execute the finalize() method, then the object is placed in the F-Queue queue, and It will be executed later by a low-priority Finalizer thread automatically created by the virtual machine. The so-called execution here means that the virtual machine starts this method, but does not promise to wait for it to finish running. The reason for this: If an object executes slowly in the finalize() method, or an infinite loop occurs (in extreme cases), it may cause other objects in the F-Queue queue to be permanently waiting, or even cause the entire memory to Recycling system crashes. The finalize() method is the last chance for the object to escape the fate of death. Later, the GC will mark the object in the F-Queue for a second small scale. If the object wants to successfully save itself in finalize()----as long as Just re-associate with anything on the reference chain, then it will be removed from the "soon to be recycled" collection when it is marked for the second time; if the object does not escape at this time, it will be recycled. Code example: Refer to the corresponding chapter of "In-depth Understanding of Java Virtual Machine"

Summary

The above is the detailed content of Detailed explanation of the marking of recycled objects in Java and the secondary marking process of objects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!