Reference types for objects in Java

Source: Internet
Author: User
Tags object object

1, strong reference usually when we program, for example: Object Object=new object (); That object is a strong reference. If an object has a strong reference, it is similar to essential necessities, and the garbage collector will never recycle it. When there is not enough memory space, the Java virtual Machine prefers to throw a outofmemoryerror error, which causes the program to terminate abnormally, and does not rely on random recycling of strongly referenced objects to resolve out-of-memory issues.

2.Soft Reference (SoftReference) If an object has only soft references, it is similar to a living thing that can be used. If the memory space is sufficient, the garbage collector does not reclaim it, and if the memory space is insufficient, the memory of those objects is reclaimed. The object can be used by the program as long as it is not reclaimed by the garbage collector. Soft references can be used to implement memory-sensitive caches. A soft reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the soft reference is garbage collected, the Java Virtual machine will add the soft reference to the reference queue associated with it.

3. Weak references (WeakReference)

If an object has only weak references, it is similar to a living thing that can be used. The difference between a weak reference and a soft reference is that an object with only a weak reference has a shorter life cycle. As the garbage collector thread scans the area of memory it governs, once an object with only a weak reference is found, its memory is reclaimed, regardless of whether the current memory space is sufficient or not.  However, because the garbage collector is a low-priority thread, it is not necessarily quick to discover objects that have only weak references. A weak reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the weak reference is garbage collected, the Java virtual machine adds the weak reference to the reference queue associated with it.

普通的对象属于强引用:String a = newString("强引用")  

Why do I need to use soft references?
First, we look at an example of an employee information query system. We will use an employee information query system implemented in the Java language to query employee profile information stored in a disk file or database. As a user, we may have to go back and look at employee profile information that was viewed a few minutes or even seconds ago (again, we often use the back button when we browse the Web page). At this point we usually have two ways of implementing the program: one is to keep the employee information viewed in the past in memory, the life cycle of each Java object that stores the employee profile information throughout the application, and the other is when the user starts to view the profile information of the other employee, Ends a reference to the Java object that stores the currently viewed employee profile information, allowing the garbage collection thread to reclaim the memory space it occupies, and reconstruct the employee's information when the user needs to browse the employee's profile information again. Obviously, the first implementation will result in a lot of memory waste, and the second implementation is flawed even if the garbage collection thread has not been garbage collected, the object containing the employee profile information is still intact in memory, and the application is rebuilding an object. We know that access to disk files, access to network resources, query databases and other operations are important factors affecting the performance of application execution, if you can regain those who have not yet been recycled Java object references, will reduce unnecessary access, greatly improve the speed of the program.

Why use weak references

Suppose an object has its own life cycle, and "useless" recycles itself, but if you add it to an array here, without a weak reference it will cause it to be reclaimed in memory and not recycled, because he is like a strong reference,

Using a weak reference does not disrupt his life cycle, but it can still have an array to get it from the array when needed

done!

Reference types for objects in Java

Related Article

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.