Java reference object softreference weakreference phantomreference (2)

Source: Internet
Author: User

Iv. Java references

Level

When is garbage collection?

Purpose

Survival time

strong

never

general state of the object

stop when JVM stops running

soft

when the memory is insufficient

simple object? Cache

terminate when the memory is insufficient

weak

During garbage collection

Object cache

end after GC operation

Illusion

Unknown

Unknown

Unknown

1. Strong reference:

Public static void main (string [] ARGs ){

Mydate date = new mydate ();

System. GC ();

}

Explanation: Even if garbage collection is explicitly calledDateIs a strong reference,DateNot recycled

2. Soft reference:

Public static void main (string [] ARGs ){

Softreference ref = new softreference (New mydate ());

Drainmemory ();//Let soft references work

}

Explanation: When the memory is insufficient, soft reference is terminated, equivalent:

Mydate date = new mydate ();

//-------------------ByJVMDecide to run-----------------

If (JVM.Insufficient memory()){

Date = NULL;

System. GC ();

}

//-------------------------------------------------------------

3. Weak references:

Public static void main (string [] ARGs ){

Weakreference ref = new weakreference (New mydate ());

System. GC ();//Make weak references work

}

Explanation: InJVMWhen garbage collection is running, the weak reference is terminated, which is equivalent:

Mydate date = new mydate ();

//------------------Garbage Collection------------------

Public void weaksystem. GC (){

Date = NULL;

System. GC ();

}

4. Hypothetical reference:

Public static void main (string [] ARGs ){

Referencequeue queue = new referencequeue ();

Phantomreference ref = new phantomreference (New mydate (), queue );

System. GC ();//Make hypothetical reference work

}

Explanation: the hypothetical reference is terminated after being instantiated. It is equivalent:

Mydate date = new mydate ();

Date = NULL;

//-------Termination point. after instantiationGCWhen the memory is insufficient--------

 

 

Http://hi.baidu.com/mynetbeans/blog/item/d72208fa8d63ac1ba9d31160.html

 

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.