当前位置:网站首页>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?
- All objects referenced in the virtual machine stack ( Local variable table in stack frame , Contains basic data types 、 Object reference )
- Method area ( Meta space ) All objects referenced by static attributes of classes in
- Method area ( Meta space ) All objects referenced by constants in
- 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 ” :
- All instances of this class have been reclaimed , That is to say Java There are no instances of this class in the heap
- Load the class ClassLoader Has been recovered .
- Corresponding to this class java.lang.Class The object is not referenced anywhere , The methods of the class cannot be accessed anywhere by reflection
边栏推荐
猜你喜欢

Décomposition détaillée du problème de chemin le plus court du graphique

VTK example -- three intersecting planes

Njuptn Nanyou Discrete Mathematics_ Experiment 4

From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time

ASP. Net programming version C (notes along with learning progress)

Installation of phpstudy

VTK例子--三个相交的平面
![[network planning] 3.2 transport layer - UDP: connectionless service](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 3.2 transport layer - UDP: connectionless service

Bluetooth development (2) -- initialization

圖的最短路徑問題 詳細分解版
随机推荐
Décomposition détaillée du problème de chemin le plus court du graphique
SQL statement -- enter the month, query the date (month, year, day), and output the month
[network planning] 1.5 seven layer network model and five layer network model
From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time
Pirate OJ 148 String inversion
[kingcraft] 3.1 link layer - functions of data link layer
12324243242
MP framework basic operation (self use)
ts+fetch实现选择文件上传
763. dividing alphabetic intervals
[network planning] 2.2.4 Web cache / proxy server
LeetCode 1996. 游戏中弱角色的数量*
From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time
Njupt Nanyou Discrete Mathematics_ Experiment 2
How about the CSC account of qiniu business school? Is it safe?
【无标题】
VTK例子--三个相交的平面
Unity custom folder icon color personalized unity compiler
Bluetooth development (3) -- look at the air bag
[network planning] 1.3 packet switching and circuit switching in the network core