Android Memory Overflow oom problem

Source: Internet
Author: User

Long time did not enter the Cnblogs, all fast grows the grass. Before docking a certain degree of requirements of our plug-in monkey test full 8 hours no Oom no crash abuse cry ... Various oom below put a note po that was written at that time to prevent the long grass.

1. What is Oom and why is Oom

Android is mainly used in embedded devices, so because of the limitations of the embedded device itself, memory is usually limited. Java has its own set of garbage collection mechanisms, but it does not mean that programs written in Java do not overflow. Java runs in a virtual machine, and the virtual machine sets an upper value for its memory (HEAP) at initialization time. This upper limit in Android is typically 16m. When a memory leak is caused by a code flaw, the leaked memory is consumed by some data objects and cannot be reclaimed by GC, resulting in an oom.

2. How to check if there is an oom problem in the project

To check if there is an oom problem in your project, you can use the DDMS and mat in eclipse to analyze the memory. We can do this:

1) Open the phone's developer options mode and connect the computer with the data cable (nonsense)

2) Open Eclipse Switch to Ddms view as

3) Click the "Update Heap" icon in the top row of icons in the Devices View interface:

Then the phone into the application to test whether there is a leaked activity or fragment, then in the Ddms right view click on the cause GC, let the virtual machine force GC once, then no point, it will automatically refresh the view (click may be stuck for a period of time this is normal) . Then observe the GC's total size and count of data ovhect. Try to close the current page and re-enter, after doing this again and again, look at count and total size after the GC will not have a noticeable fall back near the value after GC, If the memory presents a large increase trend after the GC has been manipulated many times, this page may be problematic!!

Or you can try the Mat memory analysis tool to analyze the cause of the memory leak, I'm just a few things to be interested in this tutorial to see

http://rayleeya.iteye.com/blog/1956638

3. How to Solve Oom

The oom problem that I met for the time being in these three cases

1) In some of the construction methods of a single case, the need to pass in a context, the activity of the context, resulting in a single case holding the activity of the reference can not be recycled.

For example, the Userdatabase class that operates the database, which is a singleton primarily used to open the closed database for storing data, was previously written by people in the project that ignored this, and the incoming context is acitivity because the singleton life cycle is as long as the entire program's life cycle, It is recommended to use the application context directly.

2) static member variables

Sometimes, for some reason (such as the desire to save activity initialization time, etc.), some objects are set to static, and we forget to release the references to these variables when the activity exits. Therefore, other objects that are strongly referenced by activity that are associated with this variable cannot be freed, which results in a memory leak. It is recommended to set these static variables to NULL in the OnDestroy method;

3) Registration/cancellation of listener objects

Often use some Xxxlistener objects, or Xxxobserver, Xxxreceiver objects, and then register with the Registerxxx method, with the Unregisterxxx method to log off. The usage itself is simple, but there are still some problems from the actual development of the code: the Registerxxx and unregisterxxx methods are usually called with the cursor Open/close, which is paired in the life cycle of the activity:

In the OnCreate () register, Unregitster in OnDestroy ();

In the OnStart () register, Unregitster in OnStop ();

In the Onresume () register, Unregitster in OnPause ();

Recently encountered problem, a Phonestatelistener object was defined in fragment and registered in Telephonymanager:

Telephonymanager.listen (l,phonestatelistener.listen_service_state);

However, when the activity exits, the listener is logged off, that is, the following methods are not called:

Telephonymanager.listen (L,phonestatelistener.listen_none);

Because Phonestatelistener member variable callback, is registered in the Telephonyregistry, Telephonyregistry is the background of a service will continue to run. Therefore, if you do not log off, the callback object cannot be freed, and the Phonestatelistener object cannot be freed, resulting in the activity object not being released.



Resources:

http://rayleeya.iteye.com/blog/1956059

Android Memory Overflow oom problem

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.