1. android2.x and following system optimizations:
Bitmap the decoded pixels are stored in the native heap,
The Dalvik heap has a external count that records the memory occupied by the bitmap.
A allocated>= exception is thrown when Dalvik allocated + External allocated + new OUTOFMEMORYERROR allows the maximum value to be allocated.
It must be called when it is destroyed recycle() . BitmapFactory.Optionsparameter, which hides the inNativeAlloc property,
You can make bitmap memory not counted in the Dalvik heap.
2. Android 3.x and above system optimization:
Bitmap the decoded pixels are stored in the Java Heap as long as the bitmap is not
is referenced, it is recycled when memory is tight, and does not need to be actively called recycle. When Dalvik allocated + new allocated>= is allowed to allocate the maximum value, the
The OutOfMemoryError exception is thrown, the bitmapfactory.options parameter has a inpurgeable attribute, and the pixel is stored in the native heap after it is opened.
One drawback is that when bitmap is displayed, there is no cache in the native heap, and it needs to be decoded again, blocking the UI thread.
The decoded memory can be locked in the JNI layer via androidbitmap_lockpixels, preventing it from being recycled and then passing through Androidbitmap_unlockpixels (env, ZBITMAP) when not in use.
To cancel the locking (you can refer to the fresco implementation)
3. Androidmanifest.xml Settings Largeheap=true
ActivityManager.getMemoryClass()dalvik.vm.heapgrowthlimitdthe value that can be obtained, in megabytes
ActivityManager.getLargeMemoryClass()dalvik.vm.heapsizethe value that can be obtained, in megabytes
Runtime.getRuntime().maxMemory()Gets the maximum available memory for the current VM, in units of byte
4. The creation of bitmap encapsulates a unified portal
Use LRUCache to reduce bitmap frequent create
Try to use Decodestream instead of Decoderesource decodefile
With catch, when oom occurs, the GC is actively executed, the grayscale value of the picture is lowered, and the image is created again
Decodes the corresponding size bitmap according to the actual size of the view
5. Prevent memory leaks
Avoid applications with long life cycle objects with short life cycles (using the static keyword, references in a singleton)
Note Registration and anti-registration pairing use
Use the Leakcanary tool to dynamically track memory leaks and use mat to statically analyze memory leaks.
6. Do not allocate large memory objects frequently, non-applicable objects are pre-null, and are recycled early.
Android to prevent oom optimization