Android high-efficiency loading large map, multi-graph solution, effectively avoid program Oom

Source: Internet
Author: User

We can see how much memory is available for each application using the following code

int maxmemory = (int) (Runtime.getruntime (). MaxMemory ()/1024); LOG.D ("TAG", "Max memory is" + MaxMemory + "KB");

  Bitmapfactory This class provides multiple parsing methods (Decodebytearray, DecodeFile, Decoderesource, etc.) for creating bitmap objects, and we should choose the appropriate method based on the source of the image. For example, the image on the SD card can use the DecodeFile method, the picture on the network can use the Decodestream method, the picture in the resource file can use the Decoderesource method.

  These methods attempt to allocate memory for the bitmap already built, which can easily cause oom to appear. For this reason each parsing method provides an optional bitmapfactory.options parameter that sets the Injustdecodebounds property of this parameter to true to prevent the parsing method from allocating memory for bitmap, and the return value is no longer a bitmap object, but a null 。 Although bitmap is null, Bitmapfactory.options's outwidth, Outheight, and Outmimetype properties are assigned values. This technique allows us to get the image's long and wide values and MIME types before loading the image, thus compressing the image as appropriate. As shown in the following code:

Bitmapfactory.options Options = new Bitmapfactory.options ();   Options.injustdecodebounds = true;   Bitmapfactory.decoderesource (Getresources (), r.id.myimage, options);   int imageheight = Options.outheight;   int imagewidth = Options.outwidth; String imageType = Options.outmimetype;

  So how do we compress the image?

public static int calculateinsamplesize (Bitmapfactory.options options,            int reqwidth, int reqheight) {       //source picture height and width     & nbsp;  final int height = options.outheight;       final int width = options.outwidth;       int insamplesize = 1;       if ( Height > Reqheight | | Width > Reqwidth) {           //calculates the ratio of the actual width to the width of the target            final int heightratio = Math.Round (float) height/(float) reqheight) ;           final int widthRatio = Math.Round ((float) width/(float ) reqwidth);           //selection width and high school minimum ratio as the value of Insamplesize, This will ensure the width and height of the final image    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; //must be greater than or equal to the width and height of the target.            insamplesize = HeightRatio < WidthRatio? heightratio:widthratio;       }       return insamplesize;  }

Using Picture caching technology

Memory caching technology provides quick access to images that consume valuable memory from applications. The most core of these classes is LRUCache (this class is provided in the ANDROID-SUPPORT-V4 package). This class is ideal for caching images, and its main algorithm is to store recently used objects in linkedhashmap with strong references, and to remove the least recently used objects from memory before the cached value reaches a predetermined value.

In the past, we often used an implementation of a very popular memory caching technique, either soft or weak (SoftReference or weakreference). However, this is no longer recommended because, starting with API level 9, the garbage collector is more inclined to reclaim objects holding soft or weak references, which makes soft and weak references less reliable.
In addition, in API level 11, the image's data is stored in local memory, so it cannot be released in a predictable way, potentially risking the application's memory overflow and crashing.

 

  

Android high-efficiency loading large map, multi-graph solution, effectively avoid program Oom

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.