In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of recycled object tags and object secondary tags in Java. The article is very detailed and has a certain reference value. Interested friends must read it!
I. marking of objects
1. What is a mark? How do you mark it?
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 it, there are generally two methods: reference counting and reachability analysis.
Reference counting is relatively simple to implement, which is to add a reference counter to the object, add 1 whenever there is a reference to it, subtract 1 when the reference fails, and mark it as recyclable when the counter is 0. This kind of judgment is very efficient, but many mainstream virtual machines do not adopt this method, mainly because it is difficult to solve the problem of circular references between several objects, although it is not used much, but it is still worth learning!
Public class Test {private Object obj;Public static void main () {Test t1=new Test (); Test t2=new Test (); t 1.objaccount2posit t 2.objaccount1 / whether T1 and T2 objects can be recycled System.gc ();}} if the object has gc in this row.
The basic idea of accessibility analysis is: by taking some objects called "GC Roots" as the starting point, searching from these nodes, searching for the objects that have direct or indirect reference relations with the node, and combining these objects in the form of a chain to form a "relationship network", also known as the reference chain. Finally, the garbage collector collects objects that are not on the network. As shown in the figure:
The object that connects 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 kind of object can be used as a "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 of Chapter 1). (in the stack)
2: the object referenced by the static property. (in the method area)
3: an object referenced by a constant. (in the method area)
4: the object referenced by JNI in the local method stack. (in the local method stack)
Second, the secondary recovery of objects
I said the tag of the object, but isn't it sure that if it is marked, it will be recycled? I don't know if my friends remember that the Object class has a finalize () method, and all classes inherit the Object class, so it is implemented by default.
Finalize should work like this: once the garbage collector is ready to free up the storage space occupied by the object, it first calls finalize (), and only during the next garbage collection will the object's memory be actually reclaimed. So if you use finalize (), you can do some important cleanup or cleaning during garbage collection.
When is finalize () called?
There are three situations.
1. All objects are called automatically when they are Garbage Collection, such as when running System.gc ().
two。 The finalize method is called once for each object when the program exits.
3. Explicitly call the finalize method
The purpose of this method is that the object's finalize () method is called before it is recycled. The problem here is whether there is a situation where an object is no longer in the "relationship network" (reference chain) mentioned in the previous chapter, but when the developer rewrites finalize () and adds the object back 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?
To solve this problem, the virtual machine is tagged twice, that is, objects that are not in the "relationship network" are marked for the first time. For the second time, it is necessary to determine whether the object implements the finalize () method, and if it is not implemented, it is directly determined that the object can be recycled; if it is implemented, it will be placed in a queue and executed by a low-priority thread created by the virtual machine, followed by a second small-scale tag, and this time the marked object will actually be recycled.
Summary: to put it simply, the object is tagged for the first time, and the object's finalize () method is executed before the next GC. When executing the finalize () method, it is judged whether the object implements the finalize () method, but does not achieve the direct cleanup; it does, put the object in a queue to execute the finalize method, and mark it for the second time.
Judging the reachability of objects in the java root search algorithm, it is not necessary to clean up the unreachable objects. There is a probation period at this time, and it takes at least two marking processes to judge the death of an object: if the object finds no reference chain associated with GC roots after a root search, it will be tagged for the first time and filtered once if it is necessary for the object to execute the finalize () method, if the object does not override the finalize () method, or if the finalize () method has been called by the virtual machine The virtual machine treats both situations as "unnecessary".
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 later executed by a low-priority Finalizer thread automatically created by the virtual machine. The so-called execution here refers to the method that the virtual opportunity starts, 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 if an endless 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 collection system to crash. The finalize () method is the last chance for the object to escape death. Later, GC will mark the object in F-Queue for a second small scale. If the object wants to successfully save itself in finalize (), as long as it is re-associated with any reference chain, it will be removed from the collection "about to be recycled" on the second tag; if the object does not escape at this time, it will be recycled. Code example: refer to the corresponding section of "in-depth understanding of the java Virtual Machine"
The above is all the contents of the article "sample Analysis of Recycling object tags and object Secondary tags in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.