<span id="Label3"></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>PS: in fact, this one is similar to the previous one in order to solve the problem of low memory (OOM) this happens ...<br></strong></span></p></p><p><p></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Learning Content:</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>1. Object's Reference class ....</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="color: #ff0000;">recently also through the project to know something, involving the object reference class, the Object's reference class is divided into many, strong references (in fact, is the normal reference), using SoftReference to implement soft reference,<strong>Weak Reference</strong>(weak reference) <strong>phantomrefrence (virtual Reference) ... These three reference classes I only give a detailed introduction to <span style="font-family: 楷体; font-size: 16px;"><strong>softreference implementation Soft Reference</strong></span> ... The other one is just a stroke of ....</strong> </span></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong>Strong references:<br></strong></strong></span></p></p><pre><pre>Object darker=<span style="color: #0000ff;">New</span> <span style="color: #000000;"> Object (), object darker_1</span>=<span style="color: #000000;">Darker;darker</span>=<span style="color: #0000ff;">null</span><span style="color: #000000;">;d arker_1 </span>=<span style="color: #0000ff;">null</span>;</pre></pre><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="color: #ff0000;">strong references are relatively simple, that is, after we instantiate the object, a reference to the object is a strong reference, and as long as the reference exists, then the GC (garbage Collector) will never recycle the currently referenced object ... If the reference to this object is set to null, then the GC can be used to reclaim the object ...</span><br></strong></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong>Soft References:</strong></strong></span></p></p><p><p> <span style="font-family: italic; font-size:16px;"> <strong> <strong> <span style="color: #ff0000;"> Soft reference is a part of me to elaborate, first of all, a reference purpose of soft reference, the use of soft reference is a reference to memory-linked class, mainly used in memory-sensitive cache, in fact, is often used in android, because Android virtual machine is a register-based dalvik, Its heap size is only 16M, we all know whether it is an Android application, or Java reference program, when instantiating the object is done in the heap ... So the size of this heap in Android is really very demanding, once we read the resource data is too large, or the memory of the object because the declaration of the period is too long, not be released in time, then it is likely to cause the occurrence of oom ... We all know that the GC recycling mechanism is not always working at all times in normal circumstances, and its working hours are irregular, so if our memory has exploded during the Gc's not working, it will definitely cause the program to terminate ... Oom in Android is often seen in a situation where objects are not released in a timely manner, the use of the static keyword, the thread is not controllable, and the most common is the occurrence of this situation when reading bitmap ... </span> </strong> </strong> </span> </p></p><p><p><span style="font-family: 楷体; font-size: 16px; color: #ff0000;"><strong><strong>So Oom is a very serious problem, but if the memory is not fully used, this is actually a potential problem,<span style="font-family: 楷体; font-size: 16px;"><strong>softreference (soft reference), actually give me the feeling is to solve such problems, each object through a soft reference to instantiate, Then the data of this object will be saved, when it is necessary to call the data in this object again, only through the Object's get () method to obtain the data stored in the object, in fact, it will be what we call the cache, when the memory is almost insufficient, The GC will quickly erase all soft references and free up memory space ... This not only combines the cache of the object, but also solves the occurrence of oom, double benefit ... Although it is said to be double benefit, to ensure that the memory can be used safely, then the contrary is to sacrifice efficiency, in every soft reference object is instantiated, in fact, it will take a lot of time ... So how to use it depends on the problems we Have.</strong></span></strong></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>Simple to a small code ...</strong></span></strong></strong></span></p></p><pre>Object obj=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Object (); Softrefenrence SC</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>SoftReference (obj);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">soft references are used here</span> ...</span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* During this period, there is a possibility that there will be an out-of-memory situation, then the GC will simply erase all soft references. and free up memory space * If there is enough memory space, then the GC will not work ... the work of *GC depends on the size of the memory and its internal algorithm,,,,</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(sr!=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if the soft reference still exists, then you can get the related data of this object directly ... This realizes the cache ...</span></span>obj =<span style="color: #000000;"><span style="color: #000000;">Sr.get (); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if it does not already exist, it means that the GC has recycled it, we need to instantiate the object, get the data information</span> ...</span><span style="color: #000000;"><span style="color: #000000;">obj</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Object (); SR</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">SoftReference (obj);<br>}</span></span></pre><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>Very simple piece of code, no practical meaning, just to facilitate understanding ... In the android, in fact, there are some times due to bitmap problems caused by the memory shortage, the picture is relatively large, high-pixel picture in the case of reading is very memory-intensive, if the picture is too much to read, it is more obvious, the picture too much, The memory will be eaten very tight, so we need to use soft references in the bitmap, so we can avoid the occurrence of oom ... can also effectively go to use the memory ...<br></strong></span></strong></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>Or simply say the truth, for example, we read a picture, this image is read and saved in the form of a cache ... When our application still needs the resource data of this image, we can get the resource data in the picture directly through the Get () method in the soft reference,.. So there is no need to read again, directly from the cache can be read, if the picture is too much, the object will be more and more, then when the memory will occur when oom, the GC will quickly put all the soft reference, that is, the reference and control of these objects quickly released ... Prevent the occurrence of oom ... Put a little bit of code underneath ...</strong></span></strong></strong></span></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.lang.ref.SoftReference;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.Collections;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.HashMap;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.Map;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.graphics.Bitmap;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">MemoryCache {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">encapsulates HashMap as a Thread-safe collection and uses soft references to prevent oom (out of Memory)</span> ...</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">because a large number of pictures are loaded in the LISTVIEW. in order to effectively prevent Oom from causing the program to terminate</span> ...</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>Map<string,softreference<bitmap>>cache=collections.synchronizedmap (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>hashmap<string, softreference<bitmap>><span style="color: #000000;"><span style="color: #000000;">()); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Bitmap get (String id) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">Cache.containskey (ID))</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; SoftReference</span></span><bitmap>ref=<span style="color: #000000;"><span style="color: #000000;">Cache.get (id); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">Ref.get (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">put (String id,bitmap Bitmap) {cache.put (id,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Softreference<bitmap><span style="color: #000000;"><span style="color: #000000;">(bitmap)); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Clear () {cache.clear (); }}</span></span></pre><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>This piece of code is also very simple ... is to define a set, save the soft reference of bitmap in the collection, if the picture resource exists inside the collection, then take out the operation directly ... If the storage capacity is too large: The memory limit is Exceeded. Then directly call the clear function to empty it ... This piece of code belongs to a small module in a project ....<br></strong></span></strong></strong></span></p></p><p><p></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>finally, simply say a weak reference ...</strong></span></strong></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>Weak references:</strong></span></strong></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong><span style="color: #ff0000;">weak references are not much worse than soft references, but use WeakReference to instantiate an object, and the GC is not going to specify the time to reclaim the object based on the memory reason ... A weak reference can obtain information about an object at any time before it is reclaimed by GC ... Once the GC recycles it, it gets no data about the object ... At the time of the recovery, the GC will automatically recycle it, not depending on the size of the memory to release the memory occupied by the object ... A simple example</span> ...</strong></span></strong></strong></span></p></p><pre><pre> Object obj =<span style="color: #000000;"> object A (); WeakReference WR </span> = <span style="color: #0000ff;">new </span> <span style="color: #000000;"> WeakReference (obj ); obj </span> = <span style="color: #0000ff;">null </span> <span style="color: #000000;" < span> <span style="color: #008000;">//</span> <span style="color: #008000;"> In this time period .... The GC will not be launched in a short time ... Then we can get to this object again to save the data information ... </span> <span style="color: #008000;">//</span> <span style="color: #008000;" in this period, if the gc initiates recycle, you cannot get data information that object contains ... < span> <span style="color: #0000ff;">if </span> (wr.get () ==<span style="color: #0000ff;">null </span> <span style="color: #000000;" ") {system.out.println (< span> "obj has been cleared" <span style="color: #000000;">); </span> <span style="color: #0000ff;">else </span> <span style="color: #000000;"> {System.out.println (</span> "obj has not been cleared, its information is "+<span style=" color: #000000; ">obj.tostring ()); }}</span></span></span></span></pre></pre><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>In summary, the use of soft references is to solve the situation of oom ... Avoiding oom is not just about using <span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>softreference</strong></span></strong></strong></span>, but there are other ways to reduce the use of the static keyword, and try to use the context object in the application in the app ... Avoiding thread-uncontrolled situations is a way to avoid the occurrence of oom ... Using <span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>softreference</strong></span></strong></strong></span> More is to load bitmap in the app and happen oom ... Finally give you a source Code. This source code in the previous article has been sent ... This source not only contains bitmapfactory.options, but also includes <span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>softreference</strong></span></strong></strong></span>... There are some bits and pieces of knowledge ... In short, we read this source ... can understand the truth ....<br></strong></span></strong></strong></span></p></p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong>Source:<span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><span style="font-family: italics; font-size:16px;"> <strong> <span style="font-family: italics; font-size:16px;"> <strong> Span style= "font-family: italics; font-size:16px; " > <strong> <span style="font-family: italics; font-size:16px;"> <strong> <span style="font-family: italics; font-size:16px;" <strong> <span style="font-family: italics; font-size:16px;"> <strong> <span style="font-family: italics; font-size:16px;"> <strong> Span style= "font-family: italics; font-size:16px; " > <strong> <span style="font-family: italics; font-size:16px;"> <strong> <span style="font-family: italics; font-size:16px;" <strong> <span style="font-family: italics; font-size:16px;"> <strong> <span style="font-family: italics; font-size:16px;"> <strong> Span style= "font-family: italics; font-size:16px; " > <strong> http://files.cnblogs.com/files/RGogoing/daimarufeng.zip </strong> </strong></span><strong> </strong> </strong></span><strong> </strong> </span></strong></span></strong></strong></span></strong></span></span></strong></span></strong></strong></span></strong></span> </span></strong></span></strong></span></strong></span></strong></strong></span></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><strong><span style="font-family: 楷体; font-size: 16px;"><strong></strong></span></strong></strong></span></p></p><p><p></p></p><p><p>Android Learning Note softreference Soft reference ...</p></p></span>
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