cool hit counter Back to the understanding of the four states of objects strong soft weak false references_Intefrankly

Back to the understanding of the four states of objects strong soft weak false references


I. Description of the four reference states of the object

  (located) atJDK1.2 her back,Java The concept of citation has been expanded, Classify references as strong references(Strong Reference)、 soft citation(Soft Reference)、 weak citation(Weak Reference)、 virtual citation(Phantom Reference)4 kind, these4 The intensity of the medium citation is reduced at once。

  • A strong reference is one that is prevalent in the program code, resemble"Object obj = new Object()" References of this kind If only Strong references still exist, and the garbage collector never reclaims the referenced object
  • Soft references are used to describe objects that are still useful but not necessary , for objects associated with soft references, the These objects will be reclaimed for a second time before a memory overflow exception is thrown [memory is not reclaimed without GC].。 If there is not enough memory left for this recall, Only then will a memory overflow exception be thrown。(located) atJDK1.2 her back, providesSoftReference class to implement soft references
  • Weak references are also used to describe non-essential objects, but they are a bit weaker than soft references Objects associated by weak references, can only survive until the next garbage collection occurs formerly。 When the garbage collector works, it reclaims objects that are only associated by weak references, regardless of whether there is currently enough memory. After JDK 1.2, the WeakReference class is provided to implement weak references
  • Dummy references have also become ghost references or phantom references, and they are the weakest of the reference relationships. The presence or absence of a dummy reference to an object has absolutely no effect on its survival time, nor can an object instance be obtained by dummy reference. The only purpose of having a dummy reference association for an object is to receive a system notification when the object is reclaimed by the collector。(located) atJDK1.2 her back, Provided toPhantomReference class to implement virtual references

JVM parameter initialization settings for easy code demonstration

After studying several reference states through code formerly, Define some parameters first, back The code examples in all sections of the face use these parameters。

First are the JVM parameters, here I use.

-Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=8 -XX:+UseParNewGC -verbose:gc -XX:+PrintGCDetails

This means.

  • Heap size fixed at 20M
  • With a new generation size of 10M and SurvivorRatio set to 8, the Eden area size = 8M and each Survivor area size = 1M, leaving 9M of new generation memory space available for new objects at a time
  • The new generation uses the ParNew collector, the default in Server mode is the Parallel collector, but the GC log of this collector is not as comfortable as the ParNew collector's GC log for me, so I switched to the ParNew collector
  • Print a simple message about the GC when it occurs, and print the GC details when the program finishes running

secondly, Define another constant class"_1MB":

/**
 * 1M
 */
private static final int _1MB = 1024 * 1024;

Code examples usingbyte arrays, everyonebyte because of1 byte, Therefore defining a"_1MB" It is convenient to get the constants of1M、2M、3M... The memory space in the。

Strongly cited studies

with respect to Strongly cited studies, The focus of the study was to validate" When an object is not connected by any reference chain to GC Roots, it is proven that this object is unavailable or understood as unreachable (to be reclaimed)" The correctness of this statement。 The virtual machine parameters are listed above, First write an empty method:

 1 /**
 2  * @author  May Cangjiehttp://www.cnblogs.com/xrq730/p/7082471.html
 3  */
 4 @Test
 5 public void testStrongReference0() {
 6 
 7 }

The program runs as follows.

 1 Heap
 2  par new generation   total 9216K, used 3699K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
 3   eden space 8192K,  45% used [0x00000000f9a00000, 0x00000000f9d9cdc0, 0x00000000fa200000)
 4   from space 1024K,   0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
 5   to   space 1024K,   0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
 6  tenured generation   total 10240K, used 0K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
 7    the space 10240K,   0% used [0x00000000fa400000, 0x00000000fa400000, 0x00000000fa400200, 0x00000000fae00000)
 8  compacting perm gen  total 21248K, used 4367K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
 9    the space 21248K,  20% used [0x00000000fae00000, 0x00000000fb243d88, 0x00000000fb243e00, 0x00000000fc2c0000)
10 No shared spaces configured.

This means that There's 3699K of memory space in the new generation itself . It's well understood because the VM loads a portion of data into memory when it starts, which is 3699K in size.

Our next step. Put one in.4M ofbyte Arrays go in.(4M It's because we're looking for a relatively large number., The results will be more obvious),4M=4096K, Plus the original3966K tantamount to8062K。 right for this8062K Memory space trigger onceGC:

 1 /**
 2  * @author  May Cangjiehttp://www.cnblogs.com/xrq730/p/7082471.html
 3  */
 4 @Test
 5 public void testStrongReference0() {
 6     System.out.println("********** Strong citation test( Put one in.4M array of, triggerGC)**********");
 7         
 8     byte[] bytes = new byte[4 * _1MB];
 9         
10     //  manual mode triggerGC
11     System.gc();
12 }

The results of the run are.

 1 ********** Strong citation test( Put one in.4M array of, triggerGC)**********
 2 [Full GC[Tenured: 0K->5161K(10240K), 0.0085630 secs] 7958K->5161K(19456K), [Perm : 4354K->4354K(21248K)], 0.0086002 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
 3 Heap
 4  par new generation   total 9216K, used 284K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
 5   eden space 8192K,   3% used [0x00000000f9a00000, 0x00000000f9a47300, 0x00000000fa200000)
 6   from space 1024K,   0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
 7   to   space 1024K,   0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
 8  tenured generation   total 10240K, used 5161K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
 9    the space 10240K,  50% used [0x00000000fa400000, 0x00000000fa90a548, 0x00000000fa90a600, 0x00000000fae00000)
10  compacting perm gen  total 21248K, used 4367K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
11    the space 21248K,  20% used [0x00000000fae00000, 0x00000000fb243dc0, 0x00000000fb243e00, 0x00000000fc2c0000)
12 No shared spaces configured.

To summarize the results of this GC (since this is not a study of memory allocation, we will focus only on the results and not go into detail as to why the process actually works).

  • Only 284K size objects remain in the nascent generation
  • 5161K sized objects were moved to the old age
  • 7958K objects were recycled once, leaving 5161K objects, which is equivalent to 7958 - 5161 = 1797K of space cleared by this memory collection

To sum it up. The 4M byte array is not recycled (because there are 5161K objects in total, and the VM starts with just under 5161K of 3699K loaded, so the 4M byte array must be there), The reason is that there arebytes References point to4M ofbyte arrays。 as such, We putbytes place empty Look how that turned out.:

 1 /**
 2  * @author  May Cangjiehttp://www.cnblogs.com/xrq730/p/7082471.html
 3  */
 4 @Test
 5 public void testStrongReference0() {
 6     System.out.println("********** Strong citation test( Put one in.4M array of,bytes place empty, triggerGC)**********");
 7         
 8     byte[] bytes = new byte[4 * _1MB];
 9         
10     bytes = null;
11         
12     //  manual mode triggerGC
13     System.gc();
14 }

The results of the run are.

 1 ********** Strong citation test( Put one in.4M array of,bytes place empty, triggerGC)**********
 2 [Full GC[Tenured: 0K->1064K(10240K), 0.0096213 secs] 7958K->1064K(19456K), [Perm : 4354K->4354K(21248K)], 0.0096644 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
 3 Heap
 4  par new generation   total 9216K, used 284K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
 5   eden space 8192K,   3% used [0x00000000f9a00000, 0x00000000f9a47300, 0x00000000fa200000)
 6   from space 1024K,   0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
 7   to   space 1024K,   0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
 8  tenured generation   total 10240K, used 1064K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
 9    the space 10240K,  10% used [0x00000000fa400000, 0x00000000fa50a368, 0x00000000fa50a400, 0x00000000fae00000)
10  compacting perm gen  total 21248K, used 4367K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
11    the space 21248K,  20% used [0x00000000fae00000, 0x00000000fb243dc0, 0x00000000fb243e00, 0x00000000fc2c0000)
12 No shared spaces configured.

From the GC details we can see that.

  • Older generations only used 1064K size of memory
  • The new generation uses only 284K size of memory

clearly 4M byte arrays are recycled

From this example we recall the objects that can be used as GC Roots.

  • virtual machine stack( Table of local variables in the stack frame) The object referenced in, For example, defining in the method"Object obj = new Object();"
  • Objects referenced by class static properties in the method area, For example, defining in a class"private static Object lock = new Object();", willObject Object as a lock, Shared by all classes
  • Objects referenced by constants in the method area, For example, defining in the interface"public static final char c = 'a';", word-symbol'a' It's a constant.
  • Objects referenced by JNI (i.e. Native methods in general) in the local method stack, this is not a good example to find

This recall is precisely because the first。 in itselfbytes( In the virtual machine stack) point towards4M ofbyte arrays, Since it willbytes place empty。 therefore4M ofbyte None of the arrays are available at this point asGC Roots The reference to the object points to it, i.e.4M ofbyte Arrays are marked as recyclable garbage by the virtual machine,(located) atGC The time to be recycled。

To expand a little, What the above code does here is to manually set thebytes place empty, Actually, the method call ends the same way, stack frame disappears, The disappearance of the stack frame means thatbytes disappear, or so4M ofbyte The array likewise has none that can be used asGC Roots The reference to the object points to it, So the method call ends when back,4M ofbyte Arrays are also marked as recyclable garbage by the virtual machine,(located) atGC The time to be recycled。

Research on soft citation

soft quote formerly I've said it.,JDK providesSoftReference Classes are used by co-developers, Then we'll useSoftReference Look into soft quotes, The test code is.

 1 /**
 2  * @author  May Cangjiehttp://www.cnblogs.com/xrq730/p/7082471.html
 3  */
 4 @Test
 5 public void testSoftReference0() {
 6     System.out.println("********** Soft Citation Test**********");
 7         
 8     byte[] bytes = new byte[4 * _1MB];
 9     SoftReference<byte[]> sr = new SoftReference<byte[]>(bytes);
10     System.out.println("GC formerly:" + sr.get());
11         
12     bytes = null;
13         
14     System.gc();
15     System.out.println("GC back:" + sr.get());
16 }

The same new a 4M byte array and put it into SoftReference via the SoftReference constructor.

The most noteworthy part of this code is the first9 go"bytes=null" this sentence, If you don't putbytes place empty, or so4M ofbyte The arrays also together with Strong references are associated with, Insufficient memory virtual machine will throw an exception and not try to reclaim it; willbytes place empty not the same as,4M ofbyte Arrays lose strong references, But then it's inSoftReference in, This means that this one4M ofbyte array entry formerly barely together with soft reference association

Run the program and the result is.

 1 ********** Soft Citation Test**********
 2 GC formerly:[B@76404629
 3 [Full GC[Tenured: 0K->5161K(10240K), 0.0094088 secs] 7953K->5161K(19456K), [Perm : 4354K->4354K(21248K)], 0.0094428 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
 4 GC back:[B@76404629
 5 Heap
 6  par new generation   total 9216K, used 284K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
 7   eden space 8192K,   3% used [0x00000000f9a00000, 0x00000000f9a47330, 0x00000000fa200000)
 8   from space 1024K,   0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
 9   to   space 1024K,   0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
10  tenured generation   total 10240K, used 5161K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
11    the space 10240K,  50% used [0x00000000fa400000, 0x00000000fa90a778, 0x00000000fa90a800, 0x00000000fae00000)
12  compacting perm gen  total 21248K, used 4367K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
13    the space 21248K,  20% used [0x00000000fae00000, 0x00000000fb243f10, 0x00000000fb244000, 0x00000000fc2c0000)
14 No shared spaces configured.

see thatGC formerly back,bytes all"[B@76404629", clearly4M ofbyte The arrays are not recycled。 In terms of memory space, Used in older eras5161K, harmony formerly Strong citation test It's the same., Proves it.。

So how can we see the recycling of weak references? Since weak references occur when there is not enough memory formerly, Then just keep instantiatingbyte arrays, thus back incorporate together with Soft reference association is sufficient, The code is:

 1 /**
 2  * @author  May Cangjiehttp://www.cnblogs.com/xrq730/p/7082471.html
 3  */
 4 @Test
 5 public void testSoftReference1() {
 6     System.out.println("********** Soft Citation Test**********");
 7             
 8     SoftReference<byte[]> sr0 = new SoftReference<byte[]>(new byte[4 * _1MB]);
 9     SoftReference<byte[]> sr1 = new SoftReference<byte[]>(new byte[4 * _1MB]);
10     SoftReference<byte[]> sr2 = new SoftReference<byte[]>(new byte[4 * _1MB]);
11     SoftReference<byte[]> sr3 = new SoftReference<byte[]>(new byte[4 * _1MB]);
12     SoftReference<byte[]> sr4 = new SoftReference<byte[]>(new byte[4 * _1MB]);
13     SoftReference<byte[]> sr5 = new SoftReference<byte[]>(new byte[4 * _1MB]);
14             
15     System.out.println(sr0.get());
16     System.out.println(sr1.get());
17     System.out.println(sr2.get());
18     System.out.println(sr3.get());
19     System.out.println(sr4.get());
20     System.out.println(sr5.get());
21 }

The results of the run are.

 1 ********** Soft Citation Test**********
 2 [GC[ParNew: 7958K->1024K(9216K), 0.0041103 secs] 7958K->5187K(19456K), 0.0041577 secs] [Times: user=0.05 sys=0.00, real=0.00 secs] 
 3 [GC[ParNew: 5203K->331K(9216K), 0.0036532 secs] 9366K->9481K(19456K), 0.0036694 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
 4 [GC[ParNew: 4427K->4427K(9216K), 0.0000249 secs][Tenured: 9149K->9149K(10240K), 0.0054937 secs] 13577K->13246K(19456K), [Perm : 4353K->4353K(21248K)], 0.0055600 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
 5 [Full GC[Tenured: 9149K->783K(10240K), 0.0071252 secs] 13246K->783K(19456K), [Perm : 4353K->4352K(21248K)], 0.0071560 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
 6 [GC[ParNew: 4096K->41K(9216K), 0.0010362 secs] 4879K->4921K(19456K), 0.0010745 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
 7 [GC[ParNew: 4137K->10K(9216K), 0.0009216 secs] 9017K->8986K(19456K), 0.0009366 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
 8 null
 9 null
10 null
11 [B@4783165b
12 [B@6f30d50a
13 [B@6ef2bc8d
14 Heap
15  par new generation   total 9216K, used 4307K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
16   eden space 8192K,  52% used [0x00000000f9a00000, 0x00000000f9e32560, 0x00000000fa200000)
17   from space 1024K,   1% used [0x00000000fa200000, 0x00000000fa202978, 0x00000000fa300000)
18   to   space 1024K,   0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
19  tenured generation   total 10240K, used 8975K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
20    the space 10240K,  87% used [0x00000000fa400000, 0x00000000facc3f40, 0x00000000facc4000, 0x00000000fae00000)
21  compacting perm gen  total 21248K, used 4366K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
22    the space 21248K,  20% used [0x00000000fae00000, 0x00000000fb2439e0, 0x00000000fb243a00, 0x00000000fc2c0000)
23 No shared spaces configured.

first time out8 go~ (prefix indicating ordinal number, e.g. first, number two etc)13 The result of the line looks, formerly Three 4M byte arrays are recycled finish, back Three4M ofbyte The array is still there, This proves it" Objects associated by soft references will be reclaimed when memory runs out"。

This code allows us to make a comparison by thinking about.

  • If the 4M byte array is not associated with a soft reference but with a strong reference, and the strong reference is not released, then an error is reported when the fourth 4M byte array is new, because the total old age is only 10M, the first two 4M byte arrays can enter the old age, and the third 4M byte array is put into the new age when it is new, but when the fourth 4M byte array is new, the third 4M byte array cannot enter the old age (because three 4M = 12M, which is larger than the 10M of the old age), and the VM throws OutOfMemoryError
  • in case4M ofbyte The array is associated with a soft reference and the strong reference has been released, Then it can be written infinitely"SoftReference<byte[]> sr = new SoftReference<byte[]>(new byte[4 * _1MB]);" This code., Recycle because there's not enough memory4M ofbyte arrays, There is never a possibility of memory overflow

So, Many times for non-required objects, We can take it directly together with soft reference association, This way, when there is not enough memory, the soft reference to the associated object is reclaimed without throwing itOutOfMemoryError, After all, throw itOutOfMemoryError This means that the entire app will stop running。

A study of weak citation

The JDK gives us WeakReference to associate an object to a weak reference, and the test for a weak reference is relatively simple, with the following code.

 1 /**
 2  * @author  May Cangjiehttp://www.cnblogs.com/xrq730/p/7082471.html
 3  */
 4 @Test
 5 public void testWeakReference() {
 6     System.out.println("********** Weak reference test**********");
 7         
 8     WeakReference<byte[]> wr = new WeakReference<byte[]>(new byte[4 * _1MB]);
 9     System.out.println("GC formerly:" + wr.get());
10         
11     System.gc();
12     System.out.println("GC back:" + wr.get());
13 }

I don't define a strong reference direct association here4M ofbyte arrays( Avoid forgetting to put objects together with Strongly referenced association cancellation), This is also usedSoftReference、WeakReference I personally recommend the practice of。 The result of running the program is:

 1 ********** Weak reference test**********
 2 GC formerly:[B@21dd63a8
 3 [Full GC[Tenured: 0K->1065K(10240K), 0.0080353 secs] 7958K->1065K(19456K), [Perm : 4353K->4353K(21248K)], 0.0080894 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
 4 GC back:null
 5 Heap
 6  par new generation   total 9216K, used 284K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
 7   eden space 8192K,   3% used [0x00000000f9a00000, 0x00000000f9a47318, 0x00000000fa200000)
 8   from space 1024K,   0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
 9   to   space 1024K,   0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
10  tenured generation   total 10240K, used 1065K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
11    the space 10240K,  10% used [0x00000000fa400000, 0x00000000fa50a6e8, 0x00000000fa50a800, 0x00000000fae00000)
12  compacting perm gen  total 21248K, used 4367K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
13    the space 21248K,  20% used [0x00000000fae00000, 0x00000000fb243dc8, 0x00000000fb243e00, 0x00000000fc2c0000)
14 No shared spaces configured.

see thatGC backbytes because ofnull finish, and the new generation、 I don't see any in the olden days either4M Large objects above, It's proven from both angles,GC her back 4M byte arrays are recycled finish。

Reference together withReferenceQueue

formerly The face validates the strong reference with code、 Soft applications、 There are three weak reference states, Virtual references are not demonstrated, Remember Dummy references are used to track the recycling status of objects That's enough.

Here is another knowledge point ReferenceQueue.

The role of ReferenceQueue is explained in points.

  1. SoftReference, WeakReference, PhantomReference, you can pass a ReferenceQueue through the constructor when constructing, but only for PhantomReference, the ReferenceQueue is required
  2. Take SoftReference as an example, an sr of type SoftReference is associated with a 4M byte array, then when there is not enough memory, this 4M byte array is reclaimed and sr.get() is null, which means that sr is no longer associated with this 4M byte array
  3. whilesr The corresponding 4M byte arrays are recycled her back,sr itself was joinedReferenceQueue in, Indicates that the object associated with this soft reference is reclaimed
  4. ReferenceQueue itself is a Queue, you can continuously get the queue head element through the poll() method, if it is null means that there is no soft reference associated with the object is recycled, if not null means that there is a soft reference associated with the object is recycled
  5. SoftReference looks like this, and WeakReference is the same as PhantomReference

After the theory talk, Verify it with code, Still using soft references, But to make it clearer.GC Display of relevant parameters(-verbose:gc -XX:+PrintGCDetails) strip out, The code is:

 1 /**
 2  * @author  May Cangjiehttp://www.cnblogs.com/xrq730/p/7082471.html
 3  */
 4 @Test
 5 public void testReferenceQueue() {
 6     System.out.println("********** Reference queue test**********
");
 7         
 8     ReferenceQueue<byte[]> referenceQueue = new ReferenceQueue<byte[]>();
 9         
10     SoftReference<byte[]> sr0 = new SoftReference<byte[]>(new byte[4 * _1MB], referenceQueue);
11     SoftReference<byte[]> sr1 = new SoftReference<byte[]>(new byte[4 * _1MB], referenceQueue);
12     SoftReference<byte[]> sr2 = new SoftReference<byte[]>(new byte[4 * _1MB], referenceQueue);
13     SoftReference<byte[]> sr3 = new SoftReference<byte[]>(new byte[4 * _1MB], referenceQueue);
14     SoftReference<byte[]> sr4 = new SoftReference<byte[]>(new byte[4 * _1MB], referenceQueue);
15     SoftReference<byte[]> sr5 = new SoftReference<byte[]>(new byte[4 * _1MB], referenceQueue);
16         
17     System.out.println("********** Display of objects associated with soft references**********");
18     System.out.println(sr0 + "---" + sr0.get());
19     System.out.println(sr1 + "---" + sr1.get());
20     System.out.println(sr2 + "---" + sr2.get());
21     System.out.println(sr3 + "---" + sr3.get());
22     System.out.println(sr4 + "---" + sr4.get());
23     System.out.println(sr5 + "---" + sr5.get());
24         
25     System.out.println("********** Referencing the queue ofSoftReference showcase**********");
26     System.out.println(referenceQueue.poll());
27     System.out.println(referenceQueue.poll());
28     System.out.println(referenceQueue.poll());
29     System.out.println(referenceQueue.poll());
30     System.out.println(referenceQueue.poll());
31     System.out.println(referenceQueue.poll());
32 }

The results of the run are.

 1 ********** Reference queue test**********
 2 
 3 ********** Display of objects associated with soft references**********
 4 java.lang.ref.SoftReference@50ed0a5---null
 5 java.lang.ref.SoftReference@fa4033b---null
 6 java.lang.ref.SoftReference@58d01e82---null
 7 java.lang.ref.SoftReference@4783165b---[B@6f30d50a
 8 java.lang.ref.SoftReference@6ef2bc8d---[B@23905e3
 9 java.lang.ref.SoftReference@6db17b38---[B@1f10d1cb
10 ********** Referencing the queue ofSoftReference showcase**********
11 java.lang.ref.SoftReference@50ed0a5
12 java.lang.ref.SoftReference@fa4033b
13 java.lang.ref.SoftReference@58d01e82
14 null
15 null
16 null

See due to insufficient memory, Recycled formerly Three soft references, And the three soft reference memory addresses that are recycled are right, This proves the above statement。

A study of dummy citations

I wasn't going to write a dummy quote because it was simpler to The only purpose of dummy references is simply to track the recycling of objects . However, a friend of mine raised a question later on, and I looked at it myself, and there is indeed an easy confusion with dummy references, so the article was updated to include dummy references in the last part.

The test code is.

 1 /**
 2  * @author  May Cangjiehttp://www.cnblogs.com/xrq730/p/7082471.html
 3  */
 4 @Test
 5 public void testPhantomReference() {
 6     System.out.println("********** Dummy Reference Test**********");
 7         
 8     ReferenceQueue<byte[]> referenceQueue = new ReferenceQueue<byte[]>();
 9         
10     byte[] bytes = new byte[4 * _1MB];
11     PhantomReference<byte[]> pr = new PhantomReference<byte[]>(bytes, referenceQueue);
12         
13     bytes = null;
14         
15     System.gc();
16         
17     System.out.println(referenceQueue.poll());
18     JdkUtil.sleep(100);
19     System.out.println(referenceQueue.poll());
20 }

(prefix indicating ordinal number, e.g. first, number two etc)13 gobytes place empty, At this point4M ofbyte The array is associated only with virtual references。 The key code here is17 go~ (prefix indicating ordinal number, e.g. first, number two etc)19 Three lines, code The results of the run are.

 1 ********** Dummy Reference Test**********
 2 [Full GC[Tenured: 0K->5163K(10240K), 0.0108148 secs] 7795K->5163K(19456K), [Perm : 4355K->4355K(21248K)], 0.0108713 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
 3 null
 4 java.lang.ref.PhantomReference@6f30d50a
 5 Heap
 6  par new generation   total 9216K, used 365K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
 7   eden space 8192K,   4% used [0x00000000f9a00000, 0x00000000f9a5b768, 0x00000000fa200000)
 8   from space 1024K,   0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
 9   to   space 1024K,   0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
10  tenured generation   total 10240K, used 5163K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
11    the space 10240K,  50% used [0x00000000fa400000, 0x00000000fa90acf8, 0x00000000fa90ae00, 0x00000000fae00000)
12  compacting perm gen  total 21248K, used 4371K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
13    the space 21248K,  20% used [0x00000000fae00000, 0x00000000fb244e98, 0x00000000fb245000, 0x00000000fc2c0000)
14 No shared spaces configured.

See the first time you take a virtual reference from a queue, dormancy100ms her back but fetches the dummy reference from the queue, yetGC The output is also visible, There are no more big targets in the new and old generations, instructions4M ofbyte Arrays do get recycled。

To talk about this, first look at the JDK API's description of PhantomReference.

Note the red text, the object is a virtual reachable object, the object At that time or in Israel back a certain time , it will add that reference to the queue, so when is it added to that reference queue? The answer is. When the object associated by the dummy reference is actually recycled

her formerly We said it, The only purpose of virtual references is to track the garbage collection of objects,System.gc() When the method is called,4M ofbyte The array was not immediately recycled,System.gc() The method simply sends out a notification: suggestion triggerGC。

When the 4M byte array is actually recycled, the dummy reference is added to the reference queue. So the queue head is null when it is fetched at first, and after 100ms of hibernation ensures that the object is recycled with a dummy reference in the queue head.


Recommended>>
1、Volume 2 Chapter 3 Dark Haze Looms
2、Current Situation Problems Trends and Countermeasures of Chinas Express Industry in the Context of Industrial Upgrading III
3、kotlin Android development toolbar and Actionbar set return button
4、Government information sharing strikes again free access to trademark database
5、Happy New Year to you all First thought of the new year what does blockchain mean to exist

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号