Android Exception: OutOfMemoryError

Source: Internet
Author: User

In the development of Android, with the development of the APK more and more complex, the development of the interface requirements have become higher, the use of more and more pictures, memory overflow has become a must pay attention to the problem. Let's analyze the Android memory overflow problem.

Error message:

Error/androidruntime (5084): caused by:java.lang.OutOfMemoryError:bitmap size exceeds VM budget

Error Analysis:

The Android memory overflow exception is mainly caused by the following situations:

A. The construction adapter does not use the cache contentview.
B. The cursor of the database is not closed.
C. Inputstream/outputstream is not closed.
D. Unregisterreceiver () was not called after calling Registerreceiver.
Recycle () is not called after E.bitmap is used.
F.context leaks.

The first 5 kinds of Android memory overflow situation is easy to find and solve, as long as the shut off in a timely manner, the call method call, basically can solve the problem.

Let's talk about the context leak in the Android memory overflow, let's look at an example from the Android website:

private static drawable SBGD;
@Override protected void OnCreate (Bundle state) {
Super.oncreate (state);
TextView lbl = new TextView (this);
Lbl.settext ("Leaks is bad");
if (SBGD = = null) {
SBGD = getdrawable (r.drawable.large_bmp);
}
Lbl.setbackgrounddrawable (SBGD);
Setcontentview (LBL);
}

The above code is very efficient, but the code is extremely wrong, and this code leaks the activity that was created at the start of the first screen orientation. Then when a drawable is attached to a view, the view will set it as a callback on the drawable.
The code snippet above means that drawable has a textview reference, and TextView has a reference to the activity (context type), in other words, drawable has more object references. Even if the activity is destroyed, the memory is still not released.
A reference to a context that exceeds its own life cycle can also lead to a context leak. So try to use application as the context type. This context has the same long life cycle as the application and does not depend on the activity's life cycle. If you are going to save a long time object and it needs a context, remember to use the Application object. You can easily get application objects by calling Context.getapplicationcontext () or activity.getapplication ().

Summarize the issues you should be aware of in Android development to avoid the context leak type of Android memory leak:

1. Note that the reference to the context does not exceed its own life cycle.
2. Use application for this type of context.
3.Context if the thread, must be in OnDestroy () in time to stop.
4. Use the "static" keyword carefully.

This article mainly analyzes the Android memory overflow problem in the development of Android, hoping to help the vast number of developers.

Android Exception: OutOfMemoryError

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.