Few hints to handle such error/exception for Android Apps:
-
Activities & Application have methods like:
- onLowMemory
- onTrimMemory Handle these methods to watch on memory usage.
-
tag in Manifest can have attribute 'largeHeap' set to TRUE, which requests more heap for App sandbox.
-
Managing in-memory caching & disk caching:
- Images and other data could have been cached in-memory while app running, (locally in activities/fragment and globally); should be managed or removed.
-
Use of WeakReference, SoftReference of Java instance creation , specifically to files.
-
If so many images, use proper library/data structure which can manage memory, use samling of images loaded, handle disk-caching.
-
Handle OutOfMemory exception
-
Follow best practices for coding
- Leaking of memory (Don't hold everything with strong reference)
-
Minimize activity stack e.g. number of activities in stack (Don't hold everything on context/activty)
- Context makes sense, those data/instances not required out of scope (activity and fragments), hold them into appropriate context instead global reference-holding.
-
Minimize the use of statics, many more singletons.
-
Take care of OS basic memory fundametals
- Memory fragmentation issues
-
Involk GC.Collect() manually sometimes when you are sure that in-memory caching no more needed.