A summary of some methods of processing pictures oom by Android

Source: Internet
Author: User
<span id="Label3"></p><p><p>Objective</p></p><p><p>As is known to all, each Android application has a certain memory limit at run time, and the limit is typically 16MB or 24MB (depending on the platform). therefore, in the development of applications need to pay special attention to their own memory usage, and generally the most memory resources, usually pictures, audio files, video files and other multimedia resources; since the Android system does the processing of audio, video and other resources, it does not load the entire file into Memory. There is generally no memory overflow (hereinafter referred to as Oom) errors, so their memory consumption issues are not covered in this article. This article focuses on the memory consumption of the picture, and if you are developing a picture browser application, such as an app like the one that comes with Android gallery, this problem will become particularly important, and if you are developing your current shopping client, you will sometimes encounter this problem if you do not handle it properly.</p></p><p><p>Currently encountered in the Oom scene, there are several situations, but no matter what kind of situation, the idea of solving the problem is Consistent.</p></p><p><p>(1) Display single picture, picture file volume reaches 3000*4000 level;</p></p><p><p>(2) when loading a large number of images at once in a ListView or gallery control;</p></p><p><p>Introduction to related knowledge</p></p><p><p>1. Color model</p></p><p><p>The common color model has rgb, YUV, CMYK and so on, most of the image API used in the RGB model, Android is the same, in addition, in Android there is also a color model containing transparency alpha, namely ARGB. More detailed information about the color model is not covered in the scope of this Article.</p></p><p><p></p></p><p><p>2. Digital coding of color values in computers</p></p><p><p>In the case of transparency, the color value of a pixel is represented in the computer in the following 3 ways:</p></p><p><p>(1) floating-point code: for example, float: (1.0, 0.5, 0.75), Each color component accounted for 1 float field, where 1.0 indicates that the value of the component is full red or full green or full blue;</p></p><p><p>(2) 24-bit integer encoding: For example 24-bit: (255, 128, 196), Each color component accounted for 8 bits, the value range 0-255, where 255 indicates that the value of the component is full red or full green or full blue;</p></p><p><p>(3) 16-bit integer encoding: For example 16-bit: (31, 45, 31), the 1th and 3rd color components accounted for 5 bits, the value range of 0-31, the 2nd color component accounted for 6 bits, the value range of 0-63;</p></p><p><p>In java, a variable of type float is 32 bits, a variable of type int is 32 bits, and a variable of type short and char is 16 bits, so it can be seen that the color of a pixel is encoded by floating-point notation, the memory consumption is 96 bits or 12 bytes, and the 24-bit integer notation is used to encode the As long as an int type variable, occupies 4 bytes (high 8 bits empty, low 24 bits for color), and 16-bit integer notation, as long as a short type variable, accounting for 2 bytes, so you can see the integer representation of the color value, can greatly save memory, of course, The color quality will also be relatively low. An integer encoding is generally used when acquiring bitmap in Android.</p></p><p><p>The sequence of the components of R, G and B can be in RGB or bgr,android in the notation of the above 2 integer encodings, and this article is also discussed in this Order. In 24-bit integer notation, because the r, g, B components accounted for 8 bits, Sometimes the industry also refers to RGB888 to refer to this information; similarly, in 16-bit integer notation, the R, g, and B components accounted for 5, 6, 5, respectively, Referring to this information as RGB565.</p></p><p><p>Now consider the color coding of transparency, in fact, the same way as the non-transparent encoding: the 24-bit integer encoding RGB model takes the INT type variable, its idle high 8 bits is precisely used to place the transparency component, where 0 is full transparency, 255 is completely opaque, and according to the order of a, R, G, b, Can be summed up in the case of ARGB8888, and the 16-bit integer encoding of the RGB model with the short type variable, the adjustment of the components accounted for a number of 4 bits, then just 4 bits can be vacated to encode the transparency value, according to the order of a, R, G, b, You can summarize this situation with ARGB4444. Recall that Android's Bitmapconfig class, with constants such as argb_8888, argb_4444, and RGB565, can now tell what they mean respectively. It is also possible to calculate the size of an image in memory, such as loading a 1920*1200 image with argb_8888 encoding, and presumably occupying 1920*1200*4/1024/1024=8.79mb's Memory.</p></p><p><p>3.Bitmap in-memory Storage Area</p></p><p><p>http://www.7dot9.com/2010/08/android-bitmap%E5%86%85%E5%AD%98%E9%99%90%E5%88%B6/, This article discusses the problem of Android memory limitation, The author thinks that the bitmap object is pointing to the bitmap object on the heap through a reference on the stack, and the bitmap object corresponds to a native image that uses the external store, actually using byte[] to store the memory space. however, to ensure the external allocation of memory is successful, you should ensure that the currently allocated memory plus the memory value that is currently allocated is not larger than the maximum memory value for the current heap, and that memory management completely treats external memory as part of the current heap.</p></p><p><p>Reference types for 4.Java objects</p></p><p><p>(1) Strong Reference (strongreference) 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.</p></p><p><p>(2) Soft Reference (softreference) If an object has only soft references, the memory space is sufficient, and 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.</p></p><p><p>(3) a weak reference (weakreference) weak reference differs from a soft reference in that an object with only a weak reference has a more ephemeral 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.</p></p><p><p>(4) Virtual Reference (phantomreference) "virtual reference" as the name implies, is the form of a dummy, and several other references are different, virtual reference does not determine the life cycle of the Object. If an object holds only virtual references, it can be reclaimed by the garbage collector at any time, just as there are no references.</p></p><p><p>Common Scenarios for solving oom</p></p><p><p>Memory limitations are a system-level limitation for Android applications, and as an application-layer developer, there is no way to completely eliminate this limitation, but there are some ways to use memory wisely to circumvent this problem. Here are some of the most common ways that individuals summarize:</p></p><p><p>(1) cache the image into memory, using soft reference cache to memory, instead of reloading to memory at each use;</p></p><p><p>(2) Adjust the image size, mobile phone screen size is limited, the display area assigned to the image itself is smaller, sometimes the image size can be adjusted appropriately;</p></p><p><p>(3) using low memory consumption of encoding, such as Bitmap.Config.ARGB_4444 than Bitmap.Config.ARGB_8888 more memory;</p></p><p><p>(4) Timely recovery of images, if the reference to a large number of bitmap objects, and the application does not need to display all the pictures at the same time, can be temporarily used bitmap objects timely recovery;</p></p><p><p>(5) custom heap memory allocation size, optimize the Dalvik virtual machine heap memory allocation;</p></p><p><p>This article will be the first 4 ways to do the demonstration and Analysis.</p></p><p><p>Demo Test Description</p></p><p><p>In order to illustrate the scenario of Oom and the way to solve oom, I made an Android Application--oomdemo to demonstrate that the basic situation of this application is as Follows:</p></p><p><p>(1) The application shows a gallery, the gallery load only the picture, Gallery the path of the image in the adapter, not the Picture object itself, adapter dynamically load the picture;</p></p><p><p>(2) the pictures used in the demo are pre-stored in the cache directory of sdcard, and the filenames are a.jpg,b.jpg...r.jpg, a total of 18;</p></p><p><p>(3) the picture is the JPG picture of the specification 1920*1200, the file size is within the 423KB-1.48MB range;</p></p><p><p>(4) operating environment: simulator--android2.2 version system--480*320 screen size, Moto defy--2.3.4 version CM7 system--854*480 screen size;</p></p><p><p>(5) Program Basic Structure diagram:</p></p><p><p></p></p><p><p>1. Demonstrate a</p></p><p><p>First of all, the simplest way to load images, without any image caching, resizing or recycling, simpleimageloader.class is to assume this Responsibility. The code for loading the picture section is as Follows:</p></p><p><p>@Override</p></p><p><p>Public Bitmap Loadbitmapimage (String Path) {</p></p><p><p>return Bitmapfactory.decodefile (path);</p></p><p><p>}</p></p><p><p>@Override</p></p><p><p>Public drawable Loaddrawableimage (String Path) {</p></p><p><p>return new bitmapdrawable (path);</p></p><p><p>}</p></p><p><p>Demo Result: The emulator slice can only load 1-3 sheets, then an oom error occurs, no error occurs on defy, because the memory limit is different, defy is running Third-party rom, memory allocation is 40MB. In addition gallery every time a picture, you have to Re-parse to get a picture, although there is no error on the defy, but when the image volume increased, GC recovery is not timely, there is still the possibility of oom.</p></p><p><p>2. Demo Two</p></p><p><p>Add a soft reference cache for the picture load, each time the picture gets the picture object from the cache, and if it doesn't exist in the cache, the picture is loaded from SDcard and the object is added to the Cache. also, soft-referenced objects can help the GC reclaim them when they are insufficient. Imageloaderwithcache.class responsible for this responsibility, the key code is as Follows:</p></p><p><p>Private hashmap<string, softreference<bitmap>> mimagecache;</p></p><p><p>@Override</p></p><p><p>Public Bitmap Loadbitmapimage (String Path) {</p></p><p><p>If (mimagecache.containskey (path)) {</p></p><p><p>softreference<bitmap> softreference = Mimagecache.get (path);</p></p><p><p>Bitmap Bitmap = Softreference.get ();</p></p><p><p>If (null! = Bitmap)</p></p><p><p>Return bitmap;</p></p><p><p>}</p></p><p><p>Bitmap Bitmap = Bitmapfactory.decodefile (path);</p></p><p><p>Mimagecache.put (path, New Softreference<bitmap> (Bitmap));</p></p><p><p>Return bitmap;</p></p><p><p>}</p></p><p><p>@Override</p></p><p><p>Public drawable Loaddrawableimage (String Path) {</p></p><p><p>return new bitmapdrawable (loadbitmapimage (path));</p></p><p><p>}</p></p><p><p>Demo Result: on the simulator, you can load more than 1-2 pictures on the cache, but there will still be oom; there is no error on defy. Since the images used in this case are relatively memory-based, the GC is still not able to completely avoid oom when it has not yet recovered the soft reference object, and then it has to apply for more than the remaining amount of memory space. If you load a large number of small images, such as 100*100 specifications, the role of soft references in the cache may be played Out. (this hypothesis can be further tested to prove it)</p></p><p><p>3. Demo Three</p></p><p><p>To further avoid oom, in addition to caching, images can be compressed, further saving memory, in most cases resizing the picture will not affect the Application's expressiveness. Imageloaderwithscale.class is responsible for this responsibility, the sizing code is as Follows:</p></p><p><p>Bitmapfactory.options Options = new Bitmapfactory.options ();</p></p><p><p>Options.injustdecodebounds = true;</p></p><p><p>Bitmapfactory.decodefile (path, options);</p></p><p><p>If (options.mcancel | | options.outwidth = =-1 | | options.outheight = =-1) {</p></p><p><p>LOG.D ("oomdemo", "alert!!!" + string.valueof (options.mcancel) + "" + options.outwidth + options.outheight);</p></p><p><p>Return null;</p></p><p><p>}</p></p><p><p>Options.insamplesize = util.computesamplesize (options, +, (int) (1 * 1024 * 1024));</p></p><p><p>LOG.D ("oomdemo", "insamplesize:" + options.insamplesize);</p></p><p><p>Options.injustdecodebounds = false;</p></p><p><p>Options.indither = false;</p></p><p><p>Options.inpreferredconfig = Bitmap.Config.ARGB_8888;</p></p><p><p>Bitmap Bitmap = Bitmapfactory.decodefile (path, options);</p></p><p><p>Demo Result: in the above code, the boundary of the image is decoded first, the image width and height can be obtained without the need to get the bitmap object (the width and height values are set to the Options.outwidth and options.outheight two attributes respectively). Computesamplesize the parameters for this method are "bitmapfactory.options required to parse the picture", "minimum width or high value of the resized picture", and "upper memory footprint of the resized picture". In combination with the width of the original image, This method calculates an adjustment scale, adjusts the original image and loads it into memory, and the image consumes no more memory than is previously Specified. In the simulator, when you limit the memory size of a picture to 1*1024*1024, you can load more pictures than uncompressed, but you will still get oom, and if you limit the memory size of the image to 0.5*1024*1024, you can load all the pictures completely. So resizing a picture is still a good way to save Memory. There is no error in defy, as in the same reason.</p></p><p><p>4. Demo Four</p></p><p><p>In some cases, the severe reduction of the image will affect the display of the application, so it is necessary to reduce the image as little as possible to display the picture, the manual to recycle the picture becomes particularly important. In the class imageloaderwithrecyle.class, you have added a way to reclaim your picture resources:</p></p><p><p>@Override</p></p><p><p>public void Releaseimage (String Path) {</p></p><p><p>If (mimagecache.containskey (path)) {</p></p><p><p>softreference<bitmap> reference = Mimagecache.get (path);</p></p><p><p>Bitmap Bitmap = Reference.get ();</p></p><p><p>If (null! = Bitmap) {</p></p><p><p>LOG.D ("oomdemo", "recyling" + path);</p></p><p><p>Bitmap.recycle ();</p></p><p><p>}</p></p><p><p>Mimagecache.remove (path);</p></p><p><p>}</p></p><p><p>}</p></p><p><p>Demo Result: picture compression limit is still maintained in 1*1024*1024, in adapter, call Releaseimage method in time, reclaim temporarily unwanted Picture. At this point in the simulator has never been an oom, so overall, the comprehensive cache, sizing, recycling and other means, or can effectively avoid oom.</p></p><p><p>Summary</p></p><p><p>This article introduces the soft reference cache, sizing, recycling and other means to avoid oom, the overall effect is obvious. But in the actual application scenario, the Picture's application does not want this article to demonstrate the simple, sometimes the picture resources may come from with the network, then needs to cooperate the asynchronous loading the way to download the picture first and through the callback method to display;</p></p><p><p>In addition, due to the limitations of my ability and time, this article has a lot of imperfections. For example, the understanding of the memory allocation of Android is not deep enough to explain the memory footprint of bitmap, and to optimize the heap memory allocation of Dalvik VMS by customizing the heap memory allocation size to solve oom, this article does not give a demonstration; again, for example in the demo experiment above, The details of the memory footprint are not visually displayed in the form of images, and the number of images used in the demo is too small, the specifications are single, the test environment is low, and all failed to conduct a more rigorous scientific comparative test, missing some of the Unexpected. finally, We welcome you to explore, Exchange and make Suggestions.</p></p><p><p>A summary of some methods of processing pictures oom by Android</p></p></span>

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.