Four Java reference types

Source: Internet
Author: User

Four Java reference types
Java introduced four types of references from version 1.2. The four types of references are from high to low: strong reference> soft reference> weak reference> Virtual Reference.
I. StrongReference: a strong reference is the most common reference. Generally, after the object is initialized, it is a strong reference. When an object has a strong reference, JVM does not recycle it. When the memory is insufficient, you would rather throw an OutofMemoryError to terminate the program and disable the strong reference. Ii. SoftReference: if an object is a weak reference, it will not be reclaimed if the memory is sufficient. If the memory is insufficient, it will be recycled. As long as the garbage collector does not recycle it, the program can use it. Soft references can be used to implement memory-sensitive high-speed cache. Soft references can be used together with a ReferenceQueue. If the referenced objects are recycled by the garbage collector, the Java virtual machine adds this soft reference to the reference queue associated with it. 3. WeakReference: The difference between weak reference and soft reference is that weak reference has a shorter life cycle. When the garbage collector scans the memory area, it will be recycled if it finds weak references. However, since the garbage collector is a thread with a low priority, it may not be very quick to find the weak referenced objects. Weak references can be used together with a ReferenceQueue, the Java virtual machine adds this weak reference to the reference queue associated with it. 4. PhantomReference: unlike other references, virtual references do not determine the object lifecycle. If an object only holds a virtual reference, it is the same as no reference and may be recycled by the garbage collector at any time. Virtual references are used to track the activity of objects recycled by the garbage collector. A difference between virtual and soft references and weak references is that virtual references must be used together with the reference Queue (ReferenceQueue. When the garbage collector is preparing to recycle an object, if it finds that it has a virtual reference, it will add this virtual reference to the reference queue associated with it before it recycles the object's memory.
Sample Code:
Strong reference:

public class MyClass{    MyClass()    {    }}public class Test{    public static void main(String [] args)    {        MyClass myClass=new MyClass();        System.gc();    }}
Explicit call of garbage collection, but because myClass is strongly referenced, myClass is not recycled.
Soft reference:
public class MyClass{    MyClass()    {    }}public class Test{    public static void main(String [] args)    {        MyClass myClass=new MyClass();        SoftReference ref=new SoftReference(myClass);        System.gc();    }}
When the memory is insufficient, the soft reference is recycled.
Weak reference:
public class MyClass{    MyClass()    {    }}public class Test{    public static void main(String [] args)    {        MyClass myClass=new MyClass();        WeakReference ref=new WeakReference(myClass);        System.gc();    }}
When JVM garbage collection is performed, weak references are recycled.
Virtual Reference:
public class MyClass{    MyClass()    {    }}public class Test{    public static void main(String [] args)    {        MyClass myClass=new MyClass();        ReferenceQueue rq=new ReferenceQueue();        PhantomReference pr=new PhantomReference(myClass,rq);        System.gc();    }}
Virtual references are recycled after being instantiated.
Summary:

Level

When is garbage collection?

Purpose

Survival time

Strong reference

Never

General object status

Termination when JVM stops running

Soft reference

When the memory is insufficient

Simple Object? Cache

Termination when memory is insufficient

Weak reference

During garbage collection

Object Cache

Gc termination after running

Virtual Reference

Unknown

Unknown

Unknown

 

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.