About Android using a large number of drawable to occur oom solution

Source: Internet
Author: User

1. Interface is more, and many of the interface background picture is not the same;

2. Related to skin changing function, define a variety of skins, the resources of the skin is not to use color but picture resources;

In both cases, Oom will occur if you do not pay attention to the reasonable release of memory. In the Android program, whether you set the background using layout layouts or use the Setbackgroundresource setting background, there is a greater chance of an oom on a big screen phone, especially on a Samsung's big-screen phone.

Many examples on the web use the following methods to free up memory:

    View view = Findviewbyid (R.ID.PAGE_BG);     = (bitmapdrawable) view.getbackground ();    View.setbackgroundresource (0);    Bitmapdrawable.setcallback (null);     = Bitmapdrawable.getbitmap ();     if NULL &&! bitmap.isrecycled ()) {        bitmap.recycle ();         NULL ;    }    System.GC ();    

However, there is another potentially deadly problem with this approach: using an already freed bitmap to draw the interface.

Case 1: A interface using the a.png image, and then did a jump, finish a interface, finish, using the above code to release the memory, and then again into the A interface, it is very likely that the problem occurs.

Scenario 2: A a.png image is used in a interface, and then A.png is used in the B interface, and a interface does not have a finish,b interface using Finsh, and the memory is freed using the same method as above. Then we normally, a interface will go through Onresume way to display, but at this time, our a.png in memory has been released, this time there will be the above error.

Solution:

Whether you are using the layout in XML: Android:background or in Java code: setbackground (background); setbackgrounddrawable (background); Setbackgroundresource (RESID) the way to set the background image.

When using, please call the corresponding method:
Setbackgroundresource and Android:background→setbackgroundresource (0);

Setbackgrounddrawable (background) →setbackgrounddrawable (null);

SetBackground (background) →setbackground (null);

Then call System.GC () in the ondestory;

About Android using a large number of drawable to occur oom solution

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.