Talk about WeakReference weak references

Source: Internet
Author: User

WeakReference Weak References Overview

Http://www.cnblogs.com/xrq730/p/4836700.html, four reference states for Java see this article for details

An object in Java is placed in the heap when obj is created. When the GC is running, there is no reference to obj, and the heap memory space of the Obj object is reclaimed.

But in reality, when writing code, it is often ensured that the object is recycled at the time of the next GC run by putting a null reference to an object:

Object obj = new Object ();

obj = null;

This is a way to say yes, not not, but for programmers, this is a cumbersome and not automatically recycled ideas. Manual emptying should not be done by a programmer, because for a simple object, when the method that invokes it executes, the reference is automatically popped out of the stack, and the memory is automatically reclaimed by the next GC.

But there are exceptions. such as caching, because the cache object is required by the program, so long as the program is running, the reference in the cache will not be GC, with more and more references in the cache, the GC can not reclaim more and more objects, can not be automatically recycled. When these objects need to be recycled, the task of reclaiming these objects is given to the programmer, but this violates the nature of the GC----automatically recycled.

So the weak reference weakreference is introduced in Java.

WeakReference Weak Reference Example

Here's a demonstration of WeakReference simple use, first defining an entity class:

 Public classcar{Private DoublePrice ; PrivateString color;  PublicCar (DoublePrice , String color) {         This. Price =Price ;  This. color =color; }         Public DoubleGetPrice () {returnPrice ; }     PublicString GetColor () {returncolor; }     PublicString toString () {return"This car is a" + This. Color + "car, costs $" +Price ; }}

In general, when you use WeakReference, you define a class that inherits from WeakReference, and you define some other properties in this class, and you don't define any other properties:

 public  class  Weakreferencecar Span style= "color: #0000ff;" >extends  weakreference<car>{  Public   Weakreferencecar (car car) { sup    ER   (car); }}  public  class  Weakreferencecar extends  Weakreference<car>{  Weakreferencecar (car car) { super   (ca    R); }}   

The main function calls, of course, to more clearly see the effect of the GC, set the virtual machine parameter "-xx:+printgcdetails":

 Public Static voidMain (string[] args) {Car car=NewCar (2000.0, "Red"); Weakreferencecar WRC=NewWeakreferencecar (CAR); Wrc.setstr ("111"); inti = 0;  while(true)        {            if(Wrc.get ()! =NULL) {i++; System.out.println ("Weakreferencecar s Car is alive for" + i + ", loop-" +WRC); }            Else{System.out.println ("Weakreferencecar's Car has bean collected");  Break; }        }    }

Look at the results of the operation:

Weakreferencecar's car is alive for 22880, loop-[email Protected]weakreferencecar ' s car was alive for 22881, loop-[Emai L Protected]weakreferencecar ' s car is alive for 22882, loop-[email Protected]weakreferencecar's car is alive for 22883, Loop-[email Protected]weakreferencecar ' s Car is alive for 22884, loop-[email PROTECTED][GC [psyounggen:16400k->256 K (18688K)] 16400k->256k (61440K), 0.0002863 secs] [times:user=0.00 sys=0.00, real=0.00secs] Weakreferencecar' s car is alive for 22885, loop-[email Protected]weakreferencecar's car has bean collectedHeap Psyounggen Total 18688K, used 899K [0x0000000034180000, 0x0000000035650000, 0x0000000048f80000) Eden Space 16064K,4% used [0x0000000034180000,0x0000000034220c30,0x0000000035130000) from Space 2624K,9% used [0x00000000353c0000,0x0000000035400000,0x0000000035650000) to space 2624K,0% used [0x0000000035130000,0x0000000035130000,0x00000000353c0000) Psoldgen total 42752K, used 0K [0x000000000a580000, 0x000000000cf40000, 0x0000000034180000) object Space 42752K,0% used [0x000000000a580000,0x000000000a580000,0x000000000cf40000) Pspermgen total 21248K, used 3016K [0x0000000005180000, 0x0000000006640000, 0x000000000a580000) object Space 21248K,14% used [0x0000000005180000,0x0000000005472318,0x0000000006640000)

After seeing 22,884 cycles, the Weakreferencecar associated object car is recycled, note that the object associated with the weak reference car is recycled, and not the weak reference itself, the WRC is recycled.

Other

1, in addition to weak references, there are soft references. The difference between softreference (soft reference) and WeakReference is that if the next GC is found to have no overflow, the softreference associated object is not reclaimed, but for WeakReference, At the next GC, it will be recycled, regardless of whether the memory is full. Like the cache, in fact, the most suitable for the individual think it should be softreference, because the data in the cache is used in the runtime, can not be recycled to try not to recycle them, and wait until the memory is not enough time to recycle

2, WeakReference's parent class reference has a variable queue, which is the referencequeue type. The constructor in WeakReference has a constructor for two arguments, where the second parameter is the Referencequeue, which means that after the object that WeakReference points to is recycled, You can use Referencequeue to save objects that are recycled

Talk about WeakReference weak references

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.