Weak references, java weak references

Source: Internet
Author: User

Weak references, java weak references

All the examples obtained after the actual situation are implicit references, but sometimes the large contrast to some typical images appears in the performance test, we do not want to initiate complex initialization. In this case, weak references can be used on the site.

Usage: first use the WeakReference package to check the weak referenced IsAlive consistency when the target image needs to be matched. If it is true, through the Target, you can directly obtain the actual object, saving the actual process.

Example of ticket creation:

        static void Main(string[] args)

        {

            var weakRef = GetWeakRef();

            GC.Collect();

            if (weakRef.IsAlive)

            {

                var obj = weakRef.Target as ComplexObject;

                Console.WriteLine(obj);

            }

            else

            {

                Console.WriteLine("Reference is not available.");

            }

            Console.Read();

        }

        private static WeakReference GetWeakRef()

        {

            return new WeakReference(new ComplexObject());

        }

In this example, if GC is used for garbage collection

Reference is not available.

If GC is not used for recovery

Leleapplication1.complexobject

Although all events are weak, they are not exactly the same in the case of weak events. Its purpose is to avoid Memory leakage.

An ordinary event creates a reference between the producer and the listener. If the producer wants to exist, the listener cannot be recycled, even if the listener does not have to exist at this time.

Memory leakage example:

 

The final solution method of direct mail is: the listener Automatically releases the event notification. If no reference is provided, it can be recycled.

However, when the token is used to determine the time when the event is released, you can use the weak event. The token is introduced to a third-party manager, in this way, no reference will be directly established between the sender and the listener, so as to facilitate their respective collection.

 


What is the difference between weakReference and softReference?

Weak reference objects, which do not prohibit them from indicating that objects can be terminated, terminated, and recycled. Weak references are most commonly used for standardized ing. Assume that the Garbage Collector determines that an object is weak and reachable at a certain time point. At this time, it will automatically clear all weak references to this object, and through the strong reference chain and soft reference, it can be used to obtain all weak references from any other weak reachable objects. At the same time, it declares that all the previous weak reachable objects can be terminated. At the same time or later, it adds weak references that have been registered with the reference queue to the queue. **************************************** **************************************** ***********
SoftReference:
Soft reference object. when responding to memory needs, the garbage collector determines whether to clear this object. Soft reference objects are most commonly used for memory-sensitive caching. It is assumed that the Garbage Collector determines that an object is soft reachable at a certain time point. In this case, it can automatically clear all soft references for the object, and use the strong reference chain to get all soft references for any other soft reachable object. At the same time or later, it adds soft references that have been registered with the reference queue to the queue. All soft references of soft reachable objects must be cleared before the VM throws an OutOfMemoryError. Otherwise, the time when soft references are cleared or the order in which a group of such references of different objects are cleared is not subject to any constraints. However, virtual machine implementations do not encourage the removal of recently accessed or used soft references. Direct instances of this type can be used to implement simple caching. This type or its derived subclass can also be used in larger data structures to implement more complex caching. As long as the soft reference indicates that the object is strongly reachable, that is, the actually used object will not clear the soft reference. For example, by keeping a strong indicator object for recently used items and deciding whether to discard the remaining items by the garbage collector, a complicated cache can prevent the recent items from being discarded.
**************************************** ************
WeakReference is generally used to prevent memory leakage. Ensure that the memory is recycled by the VM.

What is the difference between weakReference and softReference?

Weak reference objects, which do not prohibit them from indicating that objects can be terminated, terminated, and recycled. Weak references are most commonly used for standardized ing. Assume that the Garbage Collector determines that an object is weak and reachable at a certain time point. At this time, it will automatically clear all weak references to this object, and through the strong reference chain and soft reference, it can be used to obtain all weak references from any other weak reachable objects. At the same time, it declares that all the previous weak reachable objects can be terminated. At the same time or later, it adds weak references that have been registered with the reference queue to the queue. **************************************** **************************************** ***********
SoftReference:
Soft reference object. when responding to memory needs, the garbage collector determines whether to clear this object. Soft reference objects are most commonly used for memory-sensitive caching. It is assumed that the Garbage Collector determines that an object is soft reachable at a certain time point. In this case, it can automatically clear all soft references for the object, and use the strong reference chain to get all soft references for any other soft reachable object. At the same time or later, it adds soft references that have been registered with the reference queue to the queue. All soft references of soft reachable objects must be cleared before the VM throws an OutOfMemoryError. Otherwise, the time when soft references are cleared or the order in which a group of such references of different objects are cleared is not subject to any constraints. However, virtual machine implementations do not encourage the removal of recently accessed or used soft references. Direct instances of this type can be used to implement simple caching. This type or its derived subclass can also be used in larger data structures to implement more complex caching. As long as the soft reference indicates that the object is strongly reachable, that is, the actually used object will not clear the soft reference. For example, by keeping a strong indicator object for recently used items and deciding whether to discard the remaining items by the garbage collector, a complicated cache can prevent the recent items from being discarded.
**************************************** ************
WeakReference is generally used to prevent memory leakage. Ensure that the memory is recycled by the VM.

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.