Android Prevents Oom optimization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.