当前位置:网站首页>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
边栏推荐
- 数的奥秘之幂数与完全平方数
- Njupt Nanyou Discrete Mathematics_ Experiment 2
- Njupt South Post collection_ Experiment 2
- Njupt Nanyou Discrete Mathematics_ Experiment 1
- MD5Util
- Njupt Nanyou Discrete Mathematics_ Experiment 3
- 对接请求方式
- [database] MySQL index interview questions
- 海贼oj#448.抽奖
- From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time
猜你喜欢
![[JVM] class loading mechanism](/img/62/24b6fbec273b5cbf2338b6f4b6fe6a.png)
[JVM] class loading mechanism

海贼oj#448.抽奖

763. 划分字母区间

Dual wing layout

Bluetooth development (6) -- literacy of Bluetooth protocol architecture
![[network planning] 1.5 seven layer network model and five layer network model](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 1.5 seven layer network model and five layer network model

微信小程序实现OCR扫描识别

JVM garbage collection mechanism and common garbage collectors

市值215亿,这个四川80后会让电视机成为历史?

圖的最短路徑問題 詳細分解版
随机推荐
【无标题】6666666
Njupt Nanyou Discrete Mathematics_ Experiment 3
[untitled]
Bluetooth development (4) -- about flow control
Pirate OJ 148 String inversion
Bluetooth development (2) -- initialization
Things about Bluetooth development (10) -- getting to know ble for the first time
Bluetooth development (11) -- ble interacts happily
String time sorting, sorting time format strings
JVM garbage collection mechanism and common garbage collectors
VTK例子--三個相交的平面
Njupt Nanyou Discrete Mathematics_ Experiment 2
763. 划分字母区间
Décomposition détaillée du problème de chemin le plus court du graphique
Dual wing layout
【无标题】4555
Mysql database table backup
mybaits merge into
teterttet
[daily] robots Txt allow all search engines to include