Java Soft Reference

Source: Internet
Author: User

I. Introduction to four references in Java
1. Strong reference: Create reference to Object, GC throws Oom and does not reclaim memory
2. Soft reference: Reclaim memory before Oom
3. Weak reference: Frees memory after GC discovery
4. Virtual reference (Ghost Reference): Release after use
Second, the use of soft references
1, Purpose: Soft reference is mainly used for memory-sensitive cache, before the JVM reports out of memory will clear all soft references, so since the GC is possible to collect soft objects, may solve the memory crunch problem, avoid internal overflow.
2. Reference selection mechanism: The heap objects have strong and accessible objects, soft and accessible objects, weak and accessible objects, virtual objects, and unreachable objects. The strength, weakness, and weakness of the application are strong, soft, weak, and virtual. The most powerful reference to the object that the object belongs to.
public class SoftRefrenceDemo2 {public static void main (string[] args) {string abc=new string ("abc");  1   softreference<string> abcsoftref=new softreference<string> (ABC);  2   weakreference<string> abcweakref = new weakreference<string> (ABC);//3   abc=null;//4   Soft reference objects can be created only if the object is set to null abcsoftref.clear ();//5}}
Here's a demo for creating a soft:
public class Softrefrencedemo {public static void main (string[] args) {//Can I create a soft reference object a? A obj = new A (); softreference<a> SF = new softreference<a> (obj),//obj = null;//2, the creation of soft reference objects can be achieved only if obj is set to null? if (sf!=null) {obj = Sf.get ();} else {obj = new A (); sf = new softreference<a> (obj);}}} Class A {}
3, the browser to implement the fallback function: when the back button on the browser is taken from the cache data or rebuilt it? Look at the following two implementation strategies and issues
(1) If a Web page is recycled at the end of the browse, then pressing back to view the previously browsed page needs to be rebuilt
(2) If you store your browsed pages in memory, it can cause a lot of wasted memory and even memory overflow.
So using a soft reference to implement the browser cache can solve the two problems above.
public class SoftRefrenceDemo3 {//JS has a GoBack ()? public static void Main (string[] args) {Browser prev = new Browser ();//Get page to browse softreference<browser> sr = new Sof Treference<browser> (prev); After browsing is complete, set to soft reference if (SR! = null) {prev = (Browser) sr.get ();//Not yet recycled by the collector, get directly} else {prev = new Browser ();//due to memory constraints, the pair of soft references Like the recovery of sr = new softreference<browser> (prev); Re-build}}}class Browser {}
Third, the weak reference demo
public class Weakrefrencedemo {//When GC encounters a weakly accessible object, the object is reclaimed in a timely manner public static void main (string[] args) {string abc=new string ("ABC ");    weakreference<string> abcweakref = new weakreference<string> (ABC);    Abc=null;    SYSTEM.OUT.PRINTLN ("Before GC:" +abcweakref.get ());    System.GC ();    SYSTEM.OUT.PRINTLN ("After GC:" +abcweakref.get ());}  }

Java Soft Reference

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.