Home > Java > javaTutorial > body text

A detailed introduction to Java's four reference types

黄舟
Release: 2017-03-18 13:56:51
Original
2003 people have browsed it

What you see on paper is ultimately shallow, but you know you have to do it in detail --Lu You Ask the canal how clear it is, so that there is a source of running water --Zhu Xi

Java starts from version 1.2 Four types of references are introduced. The levels of these four references from high to low are: Strong reference>Soft reference>Weak reference>Virtual reference.

1. Strong Reference:

Strong reference is the most commonly used reference. Generally, after we initialize the object, it will be a strong reference. Quote. When an object has a strong reference, the JVM will not garbage collect it. When there is insufficient memory, it would rather throw an OutofMemoryError and let the program terminate abnormally than recycle strong references.

2. Soft Reference:

If an object is a weak reference, it will not be referenced if there is enough memory. Carry out garbage collection; if the memory space is insufficient, these objects will be recycled. As long as the garbage collector does not reclaim it, the program can use it. Soft references can be used to implement memory-sensitive caching. Soft references can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the soft reference is recycled by the garbage collector, the Java virtual machine will add the soft reference to the reference queue associated with it.

3. Weak Reference:

The difference between weak reference and soft reference is that weak reference has a shorter life cycle. When the garbage collector scans a memory area, it will be recycled whenever a weak reference is found. However, since the garbage collector is a relatively low-priority thread, it may not necessarily find those weak reference objects quickly. Weak references can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the weak reference is garbage collected, the Java virtual machine will add the weak reference to the associated reference queue.

4. Phantom Reference:

Unlike other references, phantom references do not determine the life cycle of the object. . If an object holds only phantom references, it may be reclaimed by the garbage collector at any time as if it had no references. Virtual references are mainly used to track the activities of objects being recycled by the garbage collector. One difference between virtual references, soft references and weak references is that virtual references must be used in conjunction with a reference queue (ReferenceQueue). When the garbage collector is preparing to recycle an object, if it finds that it still has a virtual reference, it will add the virtual reference to it before reclaiming the object's memory. in the associated reference queue.

Sample code:

Strong reference:

public class MyClass
{
    MyClass()
    {

    }
}public class Test
{    public static void main(String [] args)
    {
        MyClass myClass=new MyClass();        System.gc();    }
}
Copy after login

Garbage collection is explicitly called, but due to myClass is a strong reference and myClass has not been 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();    }
}
Copy after login

Soft reference is recycled when there is insufficient memory.

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();    }
}
Copy after login

Weak reference will be recycled during JVM garbage collection.

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();    
        }
}
Copy after login

Virtual reference will be recycled after being instantiated.

Summary:

Level

When to be garbage collected

Purpose

Survival time

Strong reference

Never

General state of the object

JVM terminates when it stops running

##Soft reference

When there is insufficient memory

The object is simple? Cache

Terminated when insufficient memory

Weak reference

During garbage collection

Object cache

gc terminates after running

Virtual reference

Unknown

##Unknown

Unknown

Related articles:

Detailed comparison of basic type variables and reference type variables in Java

Analysis of the concept of reference types in Java

Detailed explanation of the difference between JavaScript basic data types and reference types

The above is the detailed content of A detailed introduction to Java's four reference types. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!