The ultimate solution for Android oom problem

Source: Internet
Author: User

Everyone in the process of Android development use Bitmap, especially when the program contains a large number of images more or less will encounter Oom (Bitmap:out of Memory), encountered this problem is very painful, here to share my own on the network to find a variety of solutions, And their own research to summarize the solution.

First of all, we need to know why Oom appeared, through the Internet to check the information is due to the use of Bitmapfactory.decodefile (FilePath) This method caused, This method through the SDK provided by Google to see the code is to read the entire file directly, so occupy a large amount of resources, so found all the solution is generally directly call Bitmapfactory's underlying decode method, the code is as follows:

Bitmapfactory.options opt =Newbitmapfactory.options (); Opt.inpreferredconfig=Bitmap.Config.ARGB_8888; //A R G B//transparency Red Green blue//bitmap.config argb_4444 16 per pixel four bits//Bitmap.config argb_8888 32 per pixel eight bits//bitmap.config rgb_565 5-bit G accounted for 6 bits B accounted for 5 bits no Transparency (A)Opt.inpurgeable =true; opt.ininputshareable=true;//get a picture of a resourceFile ImageFile =NewFile (FilePath); FileInputStream FIS=NULL;Try{FIS=NewFileInputStream (ImageFile);}Catch(FileNotFoundException e) {//TODO auto-generated Catch blocke.printstacktrace ();} Bitmap BM=NULL;if(FIS! =NULL){    Try{BM= Bitmapfactory.decodefiledescriptor (Fis.getfd (),NULL, opt); }    Catch(IOException E1) {//TODO auto-generated Catch blockE1.printstacktrace (); }    finally    {        Try{fis.close (); }        Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

For the choice of bitmap.config you can choose according to their actual situation, my program is used in Bitmap.Config.ARGB_8888, although after his processing pictures occupy a relatively small amount of memory, But in other ways this parameter takes up a larger amount of memory, but in order to ensure the quality of the image or choose it.

The above content is in my own data to find the process of most of the solution, in the early development of this is really enough, and I do not realize that such a deal will have any other hidden danger or where there is no correct treatment, but to the late due to the number of client page layer, Picture also more at this time the abominable Oom again began to appear, in the memory can not find any better way, stumbled to find a problem, my bitmap created, after the use of the resources have not been released, so multiple pages of the picture is always occupy the device memory, Unconsciously, the appearance of the Oom. Want to solve this problem to see a few more advanced posts, they advocate to use the stack to allocate memory to solve this problem, see a half day also did not see understand, so used a relatively stupid method, but did not expect the effect surprisingly good, from then on, Oom completely away from me. I made the app because the page has some of the same processing, so there is a base class for the page, in the simplest way I have added in this base class

/** * Storage Bitmap */ Private New Arraylist<bitmap> ();

When I invoke the tool class, I put the newly acquired bitmap on the corresponding page, adding this sentence to the above method.

if NULL {    activity.getbitmaplist (). Add (BM);}

So I can all the bitmap through my page management collection, and finally I chose to destroy the page at the time of the page to bind all the bitmap not freed all the resources released

@Override protected void OnDestroy () {    for this. Getbitmaplist ())    {        if (!  Bitmap.isrecycled ())        {            bitmap.recycle ();        }    }     Super . OnDestroy ();}

That's it!

Although I this method will all the pages of the bitmap resources to store all of the memory is still relatively large but for the basic of this is enough, if there are more pictures to use a lot of places you can directly define a list to an array, the detection array is full, followed by release substitution.

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.