Some ideas for Android memory overflow

Source: Internet
Author: User

For Android memory Overflow This problem, small series is a headache! Here's the idea of making your own!

First memory references are divided into strong references, weak references, soft references, virtual references!

A strong reference is an instance reference that, according to the GC principle of Java, cannot be reclaimed automatically if there is a reference, so a strong reference must be =null after it is exhausted

Ex:object object = new Object ();

object = null;

Soft references are referenced on the basis of strong references, and are referenced using SoftReference, which is not recycled until the system is low on memory, and is not recycled at all, and is suitable for cache;

Ex

Object object = new Object ();

softreference<object> Objectsoft = new softreference<object> (Object);

A weak reference is an object that can be obtained at any time without affecting the GC

Ex  

Object object = new Object ();

weakreference<object> objectweak = new weakreference<object> (Object);

A virtual reference is not meant to be, after a virtual reference is made, the result is always null by the Get method, and the virtual reference usually writes the referenced object into referent, except that the Get method returns the result as null.

Object object = new Object ();

phantomreference<object> objectphantom = new Phantomreference<object> (object,referencequeue);(reference queue)

Here can be explained that imagebitmap often appear memory overflow, not the phone memory is not enough, but because the mobile phone to the application allocated memory is not all memory, but there is a limit, where the picture is generally compressed.

Another problem, the Android application layer is written in Java, and his bottom is written by C, which will cause a problem, Java has an automatic recovery mechanism, and C language does not. So you need to manually catalytic the recovery mechanism if necessary!

Ex

if (bitmapobject.isrecycled () ==false) {

Bitmapobject.recycle ();

System.GC ();//reminder System timely recovery, PS: Just a reminder, will not be executed immediately

}

The above is a small series of superficial memory overflow of some views!

Some ideas for Android memory overflow

Related Article

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.