Android resolution image download: Soft reference must understand 4 points

Source: Internet
Author: User





Android resolution image download: Soft reference must understand 4 points


Perhaps for Android developers, the word soft reference is not very familiar, soft reference in Java development with more, but, in the development of Android reference soft reference, will solve a lot of problems.



AD:





1. Strong, soft, weak, and virtual references to objects


To be more flexible in controlling the life cycle of an object, you need to know the 4 level of the object reference, from highest to lowest: strong, soft, weak, and virtual



Note: These four kinds of differences:



⑴ Strong references (strongreference)



Strong references are the most commonly used references. If an object has a strong reference, 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.



⑵ Soft Reference (SoftReference)



If an object has only soft references, enough memory space is available, the garbage collector does not recycle 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 (see below for an example). a soft reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the soft reference is reclaimed by the garbage collector, the Java Virtual machine will add the soft reference to the reference queue associated with it.



⑶ Weak references (weakreference)



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.



⑷ Virtual Reference (phantomreference)



"Virtual Reference", as the name implies, is a dummy, unlike several other references, a virtual reference does not determine the object's life cycle. If an object holds only virtual references, it can be reclaimed by the garbage collector at any time, just as there are no references. virtual references are primarily used to track activities that objects are reclaimed by the garbage collector. One difference between a virtual reference and a soft reference and a weak reference is that the virtual reference must be used in conjunction with the reference queue (Referencequeue). when the garbage collector prepares to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory.


 
   

  
  1. Referencequeue queue = new referencequeue ();
  2. Phantomreference PR = new phantomreference (object, queue);


The program can see if the referenced object is going to be garbage collected by judging whether the reference queue has been added to the virtual reference. If the program discovers that a virtual reference has been added to the reference queue, it can take the necessary action before the memory of the referenced object is recycled


2. Working principle and function of soft reference


If an object has only soft references, enough memory space is available, the garbage collector does not recycle 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.
A soft reference can be used to implement a memory-sensitive cache, and a soft reference may be combined with a reference queue (Referencequeue), and if the object referenced by the soft reference is reclaimed by the garbage collector, the Java Virtual machine will add the soft reference to the reference queue associated with it.


3.android picture download and memory management solutions for pictures in large quantities


Ideas:



(1) Download according to the path picture, save the picture in Imagecache (HashTable) after downloading, and download the picture to local;



(2) download to the No. 200 or so, the application of memory if there is insufficient (SoftReference SoftReference decided not to Oom), the Android virtual machine will start garbage collection object, this time, get the picture will appear softreference SoftReference get the picture is NULL, if NULL, you need to find the image according to the ID and path of the image has been downloaded, direct loading display, if not downloaded locally, directly from the network download;



can refer to: http://www.2cto.com/kf/201207/139035.html the picture loading this process into a downloader, focus on image download and caching problems, this is a single-threaded download, you can use multi-threaded download speed faster;



The code is as follows:


 Public Drawable loaddrawable (final String ImageUrl, final imagecallback imagecallback) {
if (Imagecache.containskey (IMAGEURL)) {
softreference<drawable> softreference = Imagecache.get (IMAGEURL);
drawable drawable = Softreference.get ();
if (drawable! = null) {
return drawable;
}
}
Final Handler Handler = new Handler () {
@Override
 Public void handlemessage (Message message) {
Imagecallback.imageloaded ((drawable) message.obj, IMAGEURL);
}
};
New Thread () {
@Override
 Public void Run () {
drawable drawable = null;
Try {
drawable = getdrawable (IMAGEURL);
Imagecache.put (IMAGEURL, new softreference<drawable> (drawable));
} catch (Exception e) {
E.printstacktrace ();
}
Message message = Handler.obtainmessage (0, drawable);
Handler.sendmessage (message);
}
}.start ();
return null;
}
Private Drawable getdrawable (String urlstring) throws Exception {
if (! Nettools.iswificonnected (context)) {
GetImage = datapreference.getisautogetpicture (context);
}Else{
GetImage = true;
}
String fileName = urlstring.substring (Urlstring.lastindexof ("/") + 1). Trim ();
File cachefile = null;
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//sdcard is mounted
Cachefile = new File (Shoppicinsdcard + fileName);
if (GetImage) {
if (!cachefile.exists () && cachefile.createnewfile ()) {//cache file does not exist && created the file, download cache image to SDcard
InputStream InputStream = getinputstreamfromhttp (urlstring);
Writecachefile2sdcard (Cachefile, Readstream (InputStream));
}
return Bitmapdrawable.createfrompath (Shoppicinsdcard + fileName);
} Else {
return null;
}
} Else {//get the data stream from the web only when there is no sdcard
if (GetImage) {
InputStream InputStream = getinputstreamfromhttp (urlstring);
return Drawable.createfromstream (InputStream, urlstring);
} Else {
return null;
}
}
}


4.android when the ListView getview the image processing, mainly to the efficiency of the ListView optimization.







From for notes (Wiz)


Android resolution image download: Soft reference must understand 4 points


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.