In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "why the citation count is still not zero after java cross-reference objects are set to null". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Reference counting algorithm (reference-counting): add a reference counter to the object, which is increased by 1 whenever a place references it; when the reference expires, the counter is subtracted by 1; the object whose counter is 0 at any time can no longer be used, and the object with a counter of 0 means that it is a garbage object and can be recycled by GC.
Reachability algorithm (GC Roots Tracing): start the search with GC Roots as the starting point, then the objects in the whole connected graph are live objects, and objects that cannot be reached by GC Roots will become objects for garbage collection and can be reclaimed by GC at any time.
The following is illustrated by a piece of code
/ * * @ description: * @ version: 1. 0 * @ author: xuanyong * @ date:2019/8/19 * / public class GcObject {public Object instance = null; private static final int _ 1MB = 1024 memory 1024; / * the only meaning of this member attribute is to take up memory so that you can see if it has been recycled in the GC log * / private byte [] bigSize = new byte [2 * _ 1MB] Public static void testGC () {GcObject obj1 = new GcObject (); / / Step 1 GcObject obj2 = new GcObject (); / / Step 2 obj1.instance = obj2; / / Step3 obj2.instance = obj1; / / Step4 obj1 = null; / / Step5 obj2 = null; / / Step6 / / suppose GC occurs in this line, can objA and objB be recycled? System.gc ();} public static void main (String [] args) {testGC ();}}
View the above code to run the GC log through IDEA (how to view the GC log by Baidu idea)
[GC (System.gc ()) [PSYoungGen: 9340K-> 824K (76288K)] 9340K-> 832K (251392K), 0.0150995 secs] [Times: user=0.00 sys=0.00, real=0.02 secs]
[Full GC (System.gc ()) [PSYoungGen: 824K-> 0K (76288K)] [ParOldGen: 8K-> 639K (175104K)] 832K-> 639K (251392K), [Metaspace: 3281K-> 3281K (1056768K)], 0.0040434 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 76288K, used 655K [0x000000076b200000, 0x0000000770700000, 0x00000007c0000000)
Eden space 65536K, 1% used [0x000000076b200000j0x000000076b2a3ee8j0x000000076f200000)
From space 10752K, 0% used [0x000000076f200000pr 0x000000076f200000pr 0x000000076fc80000)
To space 10752K, 0% used [0x000000076fc80000memori0x000000076fc80000leg0x0000000770700000)
ParOldGen total 175104K, used 639K [0x00000006c1600000, 0x00000006cc100000, 0x000000076b200000)
Object space 175104K, 0% used [0x00000006c16000000x00000006c169fde8 0x00000006cc100000)
Metaspace used 3288K, capacity 4496K, committed 4864K, reserved 1056768K
Class space used 359K, capacity 388K, committed 512K, reserved 1048576K
Process finished with exit code 0
Why the above code reference count objA and objB is not 0, and the reachability algorithm can solve this problem.
Case (1): reference counting algorithm
If you are using a reference counting algorithm:
Step5: the obj1 in the stack frame no longer points to the java heap, and the reference count of GcObject instance 1 is minus 1, resulting in 1
Step6: the obj2 in the stack frame no longer points to the java heap, and the reference count of GcObject instance 2 is minus 1, resulting in 1
At this point, it is found that the count references of GcObjcect instance 1 and instance 2 are not 0, so if the reference counting algorithm is used, the memory occupied by the two instances will not be freed, resulting in a memory leak.
Case (2): accessibility algorithm
At present, the mainstream virtual machines use GC Roots Tracing algorithm, such as Sun's Hotspot virtual machine. The core algorithm of this algorithm is to take the GC Roots object as the starting point. Using the knowledge of graph theory in mathematics, the reachable object in the graph is the living object, while the unreachable object is the garbage memory that needs to be reclaimed. There are two concepts involved here, one is GC Roots, the other is accessibility.
Then it can be used as an object of GC Roots (see figure below):
The object referenced by the local variable table of the stack frame of the virtual machine stack
The object referenced by the JNI of the local method stack
Static variables of the method area and objects referenced by constants
Objects about reachability are those that can form a connected graph with GC Roots, as shown below:
From the figure above, reference1, reference2, and reference3 are all GC Roots. You can see that:
Reference1- > object instance 1
Reference2- > object instance 2
Reference3- > object instance 4
Reference3- > object instance 4-> object instance 6
It can be concluded that object instances 1, 2, 4 and 6 all have GC Roots reachability, that is, living objects that cannot be recycled by GC.
For object instances 3 and 5, although they are directly connected, there is no GC Roots connected to them. This is the object that GC Roots cannot reach, and this is the garbage object that GC needs to collect.
At this point, I believe you should be able to fully understand the difference between reference counting algorithm and accessibility algorithm.
Looking back at the first instance, GcObject instance 1 and instance 2 are both GC Roots unreachable objects in terms of reachability algorithm, although neither of them is zero in terms of reference count.
In short, in the case of circular references between objects, the reference counting algorithm cannot recycle the two objects by GC, while the reachability algorithm can do so correctly.
This is the end of the content of "why the reference count is still not zero after java cross-reference objects are set to null". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.