当前位置:网站首页>The JVM determines whether an object can be recycled

The JVM determines whether an object can be recycled

2022-06-11 00:35:00 xujingyiss

Reference counting

Add a reference counter to the object

  • Whenever there is a place to quote it , The counter is added 1;
  • When a reference fails , The counter goes down 1;
  • Any time the counter is 0 It is impossible to use the object of .

This method is easy to implement , Efficient , But there is no such thing in the current mainstream virtual machine No, Choose this algorithm to manage memory , The main reason is that it It is difficult to solve the problem of circular references between objects .

Reachability analysis algorithm

To solve the problem of circular reference ,Java Virtual machines use reachability analysis algorithms .

The basic idea : Through a series of terms called “GC Roots” As the starting point , Start with these nodes and drill down , All objects that can be found are marked as non garbage objects . After finding all , The remaining unmarked objects are garbage objects .

Which objects can be used as GC Roots?

  1. All objects referenced in the virtual machine stack ( Local variable table in stack frame , Contains basic data types 、 Object reference )
  2. Method area ( Meta space ) All objects referenced by static attributes of classes in
  3. Method area ( Meta space ) All objects referenced by constants in
  4. Local method stack JNI(Native Method ) All objects referenced

Common reference types

java There are generally four types of references : Strong citation 、 Soft citation 、 Weak reference 、 Virtual reference .

Just focus on strong and soft references .

Strong citation Common variable references

public static User user = new User();

Soft citation : Use the object as SoftReference Object wrap of soft reference type .

Normal conditions will not be recycled , however GC After that, I found that I couldn't release space to store new objects , Then the soft reference objects will be recycled . Soft references can be used to implement memory sensitive caching .

public static SoftReference<User> user = new SoftReference(new User());

Soft references can play a role when memory resources are insufficient .

Weak reference : Use the object as WeakReference Object wrap of soft reference type , Weak references are almost like no references ,GC It will be recycled directly , Rarely used

Virtual reference : Virtual quotation is also called ghost quotation or phantom quotation , It's the weakest kind of reference , Almost no use

finalize() Methods to determine whether the object is alive or not

Even unreachable objects in the reachability analysis algorithm , It's not “ Must die ” Of , At this time they are temporarily in “ Probation ” Stage , To actually declare an object dead , At least go through the process of marking again .

When the object is not overridden finalize() Method , Objects will be recycled directly . If the object covers finalize() Method , And in this method It is associated with any object on the reference chain , Then it will be removed “ About to be recycled ” Set , Then don't recycle .

Rewriting is generally not recommended finalize() Method .

How to judge a class is useless

Method area garbage collection , Only if the class is determined to be useless , Can be recycled .

The class needs to satisfy both the following 3 It's a condition “ Useless class ” :

  1. All instances of this class have been reclaimed , That is to say Java There are no instances of this class in the heap
  2. Load the class ClassLoader Has been recovered . 
  3. Corresponding to this class java.lang.Class The object is not referenced anywhere , The methods of the class cannot be accessed anywhere by reflection
原网站

版权声明
本文为[xujingyiss]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020628160499.html