Many textbooks use the following algorithm to determine whether an object is alive: Add a reference counter to an object, and Add 1 to the counter value whenever it is referenced. When the reference fails, the counter value is reduced by 1. Objects whose counters are 0 at any time cannot be used again. Many fresh graduates and developers with many years of experience give this answer to this question.
Objectively speaking, the implementation of the Reference counting algorithm is safe, and the determination efficiency is also very high. In most cases, it is a good algorithm and there are some well-known application cases, for example, Microsoft COM (Component Object Model) technology, Flash Player using actionscript3, Python language, and squirrel widely referenced in the game script field all use the reference counting algorithm for memory management. However, the reference counting algorithm is not used in Java to manage the memory. The main reason is that it is difficult to solve the issue of cross-cycle reference between objects.
In the following code, the object obja and objb both have the field instance, and the value assignment order is obja. instance = objb and objb. instance = obja. In addition, the two objects have no reference. In fact, they are no longer accessible, but because they reference each other, as a result, their reference count is not 0, so the reference counting algorithm cannot notify the GC collector to recycle them.
Package GC;/*** reference the counting algorithm GC * @ author Madison * @ date 2014-7-12 * after the testgc () method is executed, will obja and objb be GC? */public class referencecountinggc {public object instance = NULL; Private Static final int _ 1 MB = 1024*1024; // The unique significance of this Member attribute is to occupy the memory, so that you can see in the GC log whether the private byte [] bigsize = new byte [2 * _ 1 MB] has been recycled; public static void testgc () {referencecountinggc obja = new referencecountinggc (); referencecountinggc objb = new referencecountinggc (); obja. instance = objb; objb. instance = obja; obja = NULL; objb = NULL; system. GC ();}}
Running result:
[Full GC (system) [tenured: 0 k-> 153 K (10944 K), 0.0052420 secs] 4409 K-> 153 K (15872 K), [perm: 381 K-> 381 K (12288 K)], 0.0052783 secs] [times: User = 0.00 sys = 0.00, real = 0.00 secs]
Heap
Def New Generation total 4992 K, used 179 K [0x241b0000, 0x24710000, 0x29700000)
Eden space 4480 K, 4% used [0x241b0000, 0x241dcda8, 0x24610000)
From space 512 K, 0% used [0x24610000, 0x24610000, 0x24690000)
To space 512 K, 0% used [0x24690000, 0x24690000, 0x24710000)
Tenured generation total 10944 K, used 153 K [0x29700000, 0x2a1b0000, 0x341b0000)
The space 10944 K, 1% used [0x29700000, 0x29726430, 0x29726600, 0x2a1b0000)
Compacting perm Gen total 12288 K, used 381 K [0x341b0000, 0x34db0000, 0x381b0000)
The space 12288 K, 3% used [0x341b0000, 0x3420f4e0, 0x3420f600, 0x34db0000)
RO space 10240 K, 55% used [0x381b0000, 0x38733dd8, 0x38733e00, 0x38bb0000)
RW space 12288 K, 55% used [0x38bb0000, 0x39256cd0, 0x39256e00, 0x0000b0000)
From the running results, we can clearly see that the GC log contains "4409 K-> K", which means that the virtual machine does not recycle the two objects because they are referenced by each other, this also shows that the virtual machine does not reference the counter algorithm to determine whether the object is alive.
I want to know what to do later and listen to the next Decomposition
This article is from the "2377209" blog. For more information, contact the author!