Various Java reference and garbage collection mechanisms

Source: Internet
Author: User
Today I have learned a lot about various references in Java.
It can be divided into the following types:
1. Strong reference
2. Soft reference softreference
3. Weak reference weakreference
4. Virtual Reference phantomreference

5. finalreference all objects that implement the finalize () method
The JDK of Oracle JDK. IBM may be somewhat different.
Strong reference:
String a = new String("A");

General programs are used. When too many applications are applied, an outofmemory exception occurs. GC will not be released automatically.

Soft reference:

SoftReference<String> a = new SoftReference<String>(new String("A"));String real = a.get(); // if memory is near/equal to threshold of memory, real may be null.

GC is automatically released. There will be no outofmemory exception, but it will be put into the reference queue referencequeue before/finalize.
Suitable for cache.

Weak reference:

WeakReference<String> a = new WeakReference<String>(new String("A"));String real = a.get(); // may null

GC is automatically released. There will be no outofmemory exception, but it will be put into the reference queue referencequeue before/finalize.
It is suitable for cache, And the meaning here is: If you can see this cache object in the memory, it is best not to see it, load this object will not lose a lot of efficiency.

Virtual Reference:

PhantomReference<String> a = new PhantomReference<String>(new String("A"));String real = a.get(); // is always null

GC will not be automatically released, and there will be outofmemory. Objects are placed in referencequeue only after garbage collection/finalize. This ensures that the object will not be referenced again. It can also be used to determine whether the object has been removed from the memory.

Finalreference:
It cannot be used directly. This subclass of finalizer cannot be used directly. The system starts finalizerthread at runtime. This thread is used to clear objects in referencequeue. First, call the finalize () method of the object, and then assign a null value to the reference. This removes the relationship between the reference and the object. The referencequeue can be regarded as the queue object where all the above references are finally put. Because all objects have the finalize () method.


In addition, there are two steps for garbage collection: the first step is to determine the garbage collection object, and the second step is to call the finalize () method to remove the object from the memory. Before the finalize () method is called, a strong reference can recall an object so that the object will not remove the memory. This will cause garbage collection to be repeated multiple times and the object cannot be removed. However, if you use virtual references, you can ensure that the objects in the referencequeue must have been removed from the memory.

Refer:
Http://www.ibm.com/developerworks/cn/java/j-lo-langref/
Https://weblogs.java.net/blog/2006/05/04/understanding-weak-references
Http://yangguangfu.iteye.com/blog/849317

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.