On the process of Java recycling object tags and object secondary Tags

1、 Tags for objects

1. What is a tag? How?

The first problem, as we all know, 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.

The implementation of reference counting is relatively simple. It is to add a reference counter to the object. Whenever there is a place to reference it, it will increase 1, decrease 1 when the reference fails, and mark it recyclable when the counter is 0. This 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 reference between several objects. Although it is not used much, it is still worth learning!

The basic idea of reachability analysis is to search from these nodes by taking some objects called "GC roots" as the starting point, search for objects with direct or indirect reference relationship with the node, and combine these objects in the form of chain to form a "relationship network", also known as reference chain. Finally, the garbage collector recycles some objects that are not on the network. As shown in the figure:

The object connecting the GC roots object is determined to be alive, while the die obj on the right is marked as recyclable because it has nothing to do with gcroots. At present, the mainstream commercial virtual machines use similar methods. So what object can be used as "GC roots"? In Java, there are four kinds of objects that can be used as "GC roots"

1: The reference object in the stack frame (the noun in Chapter 1)

2: Objects referenced by static properties. (in the method area)

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

4: The object referenced by JNI in the local method stack. (in the local method stack)

2、 Secondary recycling of objects

Said that the object is marked, but if it is not marked, it will be recycled? I wonder if my friends remember that the object class has a finalize () method. All classes inherit the object class, so this method is also implemented by default.

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

There are three situations

1. All objects are automatically called when garbage collection is used, such as running system When GC ()

2. Call the finalize method once for each object when the program exits.

3. Explicitly call the finalize method

The purpose of this method is: before the object is recycled, the finalize () method of the object will be called. Before recycling here, it means after being marked. The problem is here. Is there a case that an object is no longer in the "relationship network" (reference chain) mentioned in the previous chapter, but after the developer rewrites finalize() and adds the object to the "relationship network", that is, the object is still useful to us and should not be recycled, But it has been marked. What should I do?

To solve this problem, the virtual machine is marked twice, that is, the object that is not in the "relationship network" is marked for the first time. The second time, you must first judge whether the object implements the finalize () method. If not, you can directly judge whether the object is recyclable; If it is implemented, it will be placed in a queue and executed by a low priority thread established by the virtual machine, and then the second small-scale marking will be carried out. This time, the marked objects will be really recycled.

Summary: in short, the object is marked for the first time, The finalize () method of the object will be executed before the next GC. Judge whether the object implements finalize when executing the finalize () method () method, which does not implement direct clearing; it implements the method, which puts the object in a queue, executes the finalize method, marks the object for the second time, judges the reachability of the object in the Java root search algorithm, and does not necessarily have to clean up the unreachable object. At this time, there is a probation period, and the real judgment of an object's death must go through at least two marking processes: for example If the object finds no reference chain associated with GC roots after root search, it will be marked and filtered for the first time, The filter condition is whether it is necessary for this object to execute the finalize () method. When the object does not overwrite the finalize () method, or the finalize () method has been called by the virtual machine, the virtual machine regards both cases as "unnecessary execution".

That is, when an object overrides the finalize () method, the object is determined to be necessary to perform finalize () method, then the object is placed in the f-queue queue and executed later by a low priority finalizer thread automatically established by the virtual machine. The so-called execution here means that the virtual opportunity starts the method, but does not promise to wait for it to run. The reason for this: if an object executes slowly in the finalize () method, Or an dead loop occurs (in extreme cases), which may cause other objects in the f-queue queue to be permanently waiting, and even cause the whole memory recovery system to crash. The finalize () method is the last chance for the object to escape the fate of death. Later, GC will mark the objects in the f-queue for the second time in a small scale if the objects are to be finalized Save yourself successfully in () -- as long as you re associate with any in the reference chain, it will be moved out of the collection of "about to be recycled" at the second marking; if the object does not escape at this time, it will be recycled. Code example: refer to the corresponding chapter of "understanding Java virtual machine in depth"

The above is all about the process of recycling object tags and object secondary tags in Java. I hope it will be helpful for everyone to learn Java. Interested friends can refer to: Java virtual machine loading and initializing a class class code analysis, detailed explanation of accommodation examples of Java programming thought objects, detailed explanation of high concurrency solutions of Java system, etc. you can leave a message at any time. Xiaobian will reply to you in time.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>