Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method to determine that an object needs to be recycled in JVM?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "what is the method to determine the object needs to be recycled in JVM", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the method to determine the object needs to be recycled in JVM" can help you solve your doubts.

Citation counting method

There is a reference count on each object. Every time the object is referenced, the reference counter is + 1, the object reference is released, and the reference counter is-1. Until the reference count of the object is 0, the object identifies that it can be recycled.

This can be represented by the graph in the data algorithm, object A-object B-object C has references, so it will not be recycled. Because object B is not referenced, there is no path to object B, and the reference count of object B is 0. Object B will be recycled.

However, this algorithm has obvious drawbacks. In the case of circular references, the objects referenced by the loop will not be recycled. For example, the following figure: object A, object B circular reference, no other object references An and B, then An and B will not be recycled.

Root search algorithm

This algorithm currently defines several root, that is, these objects are objects that will not be recycled by the jvm virtual machine, so the objects referenced by these objects are all objects in use, and the unused objects of these objects are the objects that are about to be recycled. To put it simply: if the object can reach the root, it will not be recycled; if the object cannot reach the root, it will be recycled.

Classes loaded and objects created by the startup class (bootstrap loader)

Objects referenced by jvm runtime method zone class static variables (static)

Jvm runtime methods go to objects referenced by constant pooling

Objects referenced by the virtual machine stack variable table in the current running thread of jvm

Objects referenced in the local method stack (jni)

Jvm uses the root search algorithm to determine whether to collect objects or not.

Add: jvm judges the recovery of objects

Reachability analysis algorithm

Reachability analysis algorithm: through a series of root objects of "GC Roots" as the starting node set, searching down according to the reference relationship, if an object is not connected to the root object with any reference chain, the object is unreachable.

However, the objects that are unreachable after reachability analysis do not have to be recycled, and will go through a secondary marking process.

Secondary marking

1. If the object is unreachable after the reachability analysis, it is marked for the first time. The filter is then performed on whether or not to execute the finalize () method.

If the object does not override the finalize () method, or if finalize () has already been called once, you do not need to execute the finalize () method. Then this object is determined to need to be recycled.

(the finalize () method of the object will only be called by the system once, and finalize () will not be executed the next time the object is reclaimed.)

If the object overrides the finalize () method and the finalize () method has not been called, you need to execute the finalize () method.

two。 If the object needs to execute the finalize () method, the object is placed in a queue of F-Queue, and a finalizer thread executes the finalize () method of these objects.

3. Then the collector will heap the object of the F-Queue queue for secondary marking. If the object fails to escape in the finalize () method, the object will be marked twice, and the object marked twice will be determined to need to be reclaimed.

(an object can establish a reference relationship with an object on the reference chain in the finalize () method, so that on the second tag, the collector will move it out of the collection of recycled objects to escape.)

After reading this, the article "what is the method of determining objects that need to be recycled in JVM" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report