How To Deal With Out of Memory Exception In Android

Page 1

How To Deal With Out oF Memory Exception In Android In this blogs we have explained about memory exception and how to deal with “out of memory exception in android”. In android we encounter with ​ out of memory exception​ many times. The

reasons are we don't have expanding memory and completely dependent on the available phone memory. So we don't have control over external factors and will need to optimize memory at our end.

Reasons of Out of Memory Error :: 1.Biggest reason is memory leak i.e, Context leaking or can say Activity Leaking,a Service has the same problems as Activity in this regard. 2.You are doing process that demands continuous memory and at a point it goes beyond max memory limit of a process. 3.When you are dealing with large Bitmap and load all of them at run time. There are some technique to optimizing the memory ::


​ 1.Flag Technique : ​ A solution is to set android:hardwareAccelerated=”true” and android:largeHeap=”true” It is available after android 3.0 and so many android devices still run on gingerbread(2.3+) so it is not a reliable solution. ​ 2.DVM garbage collection (Memory Leakage) : ​ DVM Garbage collection works same as JVM garbage collection, Make sure that whenever you are done with object and dont need it anymore, so reference as null.

3​ .Out of memory exception when we load image on imageview in our listview.Images come in all shapes and sizes.In many cases It might be larger than required for a typical appication UI. When Decode Image normally:: 1. String sdImage = "/sdcard/imageTesting/" + imageTestName; 2. Bitmap bitmap = BitmapFactory.decodeFile(sdImage);

Most of the Time It give Out of memory Exception when image size is large. So we can use bitmap Option. 1. String sdImage = "/sdcard/imageTesting/" + imageTestName; 2. 3. BitmapFactory.Options ourOptions=null; 4. 5. options = new BitmapFactory.Options(); 6. 7. ourOptions.inSampleSize = 2; 8.


9. Bitmap bitmap = BitmapFactory.decodeFile(sdImage, null, ourOptions);

But It is not the Best Solution….

Read full blogs :​ ​ How to deal with Out of Memory Exception in Android You can check more informative blogs and tutorials at android development blogs section and can also browse the ​ android developer forum​ ​ for posting and viewing latest questions on iphone development


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.