1. Causes of memory overflow
1.1. Memory leaks
The difference between a memory leak and a memory overflow:
Memory leak: a reference to a useless object exists in the program, which prevents the GC from being recycled . a memory leak can eventually cause oom.
Memory overflow: When the program requests memory, it does not have enough memory space for it to use, and an out of memories appears.
1.2. Save multiple objects that consume too much memory
some of the application's logical operations consume large amounts of memory (such as loading a large, non-processed, ultra-high-definition picture, etc.), resulting in exceeding the threshold value .
2. Memory optimization
2.1, Bitmap
1) compress the bitmap
through bitmapfactory.options setting insamplesize sampling rate, and set injustdecodebounds to true after the picture is not loaded into memory, just calculate the original image size, after the image compression processing is complete, set to false can be loaded into memory.
2) recycling of bitmap
When the Bitmap object is no longer in use, call the bitmap.recycle () method to release The memory space that Bitmap occupies , and a null reference is made so that the GC can be recycled.
3) bitmap to cache
The Bitmap cache is divided into two types: the memory cache and the hard disk cache.
Memory Cache (LruCache):
LruCache The Bitmap class is ideal for use as a caching task, and it stores the most recently referenced objects in a strongly referenced Linkedhashmap , and the most recently used objects are freed after the cache has exceeded the specified size.
2.2. Modifying object reference types
Reference type:
The reference is divided into four levels, which are four levels from highest to lowest: Strong reference > Soft reference > Weak reference > Virtual Reference.
1) strong references (strong reference "
such as: object object=new object (), object java virtual machines would rather throw outofmemoryerror error, causing the program to terminate abnormally, You will not be able to easily reclaim objects with strong references to resolve out-of-memory issues.
2) Soft References (softreference)
Only when the memory is not enough to recycle , often used in the cache, when the memory reaches a threshold, theGC will recycle it;
3) Weak references (weakreference)
Weakly referenced objects have a more ephemeral life cycle. As the garbage collector thread scans the area of memory it governs, once an object with only a weak reference is found, its memory is reclaimed, regardless of whether the current memory space is sufficient or not.
4) Virtual Reference (phantomreference)
" Virtual reference " as the name implies, is the form of a dummy, virtual reference does not determine the life cycle of the object. If an object holds only virtual references, it can be garbage collected at any time, just as there are no references.
butforSoftReference(Soft References) or WeakReference(Weak references) Bitmap caching scheme is deprecated now. From Android2.3version ( API Level 9 Start, the garbage collector focuses more on recovering soft / weak references .
2.3, Adapter Adapter
1) Convertview for reuse
The number of times the Convertview layout file is reused because the Viewis recreated each time the getView is called. So the previous view may not have been destroyed, coupled with the constant new view is bound to cause memory leaks ;
2) Use viewhoder mode
Using viewhoder can reduce The number of Findviewbyid, because by Settag The view's tag Deposit A data structure that contains a reference to the view that we want to bind the data to;
2.4. Other
1) Try to eat less enmu
enumerations require twice or more memory relative to static constants .
2) Resource Release
Remember to close resources when using the database,IO stream .
Memory optimization for Android performance optimization