Out of memory error is very common error when you are developing for a application that deals with multiple images sets or large bitmaps or some Animation stuff. In this case we have to be very careful and efficient while handling the images or object allocation and deallocation. OOM error comes when the allocation crosses the heap limit or your process demand a amount of memory that crosses the heap limit.
In Android, every application runs in a Linux Process. Each Linux Process has a Virtual Machine (Dalvik Virtual Machine) running inside it. There is a limit on the memory a process can demand and it is different for different devices and also differs for phones and tablets. When some process demands a higher memory than its limit it causes a error i.e Out of memory error.
Possible Reasons:
There are number of reasons why we get a Out of memory errors. Some of those are:
1. You are doing some operation that continuously demands a lot of memory and at some point it goes beyond the max heap memory limit of a process.
2. You are leaking some memory i.e you didn’t make the previous objects you allocated eligible for Garbage Collection (GC). This is called Memory leak.
3. You are dealing with large bitmaps and loading all of them at run time. You have to deal very carefully with large bitmaps by loading the size that you need not the whole bitmap at once and then do scaling.
http://blogs.innovationm.com/android-out-of-memory-error-causes-solution-and-best-practices/
Yes, you can get memory info programmatically and decide whether to do memory intensive work.
Get VM Heap Size by calling:
Runtime.getRuntime().totalMemory();
Get Allocated VM Memory by calling:
Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
Get VM Heap Size Limit by calling:
Runtime.getRuntime().maxMemory()
Get Native Allocated Memory by calling:
Debug.getNativeHeapAllocatedSize();
I made an app to figure out the OutOfMemoryError behavior and monitor memory usage.
You can get the source code at https://github.com/coocood/oom-research
No comments:
Post a Comment