Android Memory leak analysis brief idea

Source: Internet
Author: User


Work encountered a lot of memory leaks need to analyze the situation, and now roughly briefly write down ideas, and so on after relatively ample time to supplement.


1. Understand the basis of memory leakage?

The personal summary is: continuously increases, only increases does not reduce!

Understanding these 8 words, with a few commands and tools to determine whether your application has a memory leak problem, it is very important, if the first judgment is wrong, then there is no reason to continue down.

The command is as follows:

ADB shell

Dumpsys meminfo App Package name [of course, more roughly, you can use the ADB shell Procrank]

In this case you can see a Memory usage table



And the first thing we focus on is the total value, for example you will execute a script, or monkey is constantly operating your application, you can observe the total in half an hour, if it continues to increase, then there is a memory leak problem, but it is not absolute, At this time you should cooperate with the DDMS tool to do analysis. (There is a heap inside the DDMS, which can cause GC, if you find that after you have performed the GC, there is still no significant reduction in memory, then congratulations, this part of the memory leaks.) )


2. According to a situation described in the first step, it is possible to determine if there is a memory leak, then what needs to be done?

If memory leaks, what should we do?

Idea: Must we want to find the leak point, then how to find?


(1). You need to crawl the Hprof file, this file can show some memory usage, but remember, you do not clear the process to catch the file, and then with the mat to the Hprof file analysis

(Mat analysis of the Hprof file in this area of knowledge is too complete, readers can learn from their own ha, here is not cumbersome)


(2). If mat analysis out of the east, you really do not see anything fishy, then congratulations again, you big recruit.

At this point you will continue to observe the Dumpsys Meminfo package name, the output of the results of the information, the focus on the unknown line and the Native heap, focus on the heap Alloc or Pss total,

If your total totals continue to increase, but because of the increase in these two lines, you do not need to continue to spend time on the mat because of this memory leak, out of the native layer (C)

Then you need to find out where you use JNI in your program, such as the so library or some other special calls, to analyze whether they can cause memory leaks.


(3) Of course you still have no clue, so it doesn't matter, another command is for you and exist, (first an application of the PID number, with Dumpsys meminfo package name, there can be found)

For example, the MMS above me, the PID number is 2786, thenadb Shell Showmap-a PID number (adb Shell Showmap-a 2786)

Then according to the result [...] This information, look for keywords on google, such as:[anon] bash heap


(4) When you end up not knowing which side of the. So library is causing you to see the memory allocations for the native heap, you still need to use DDMS

You need to execute the following command first:

adb shell setprop libc.debug.malloc 1

ADB shell stop

ADB shell start


Then you need to change the configuration parameter value in Eclipse "because if you don't configure it, your DDMS opens by default it doesn't see the tab entry for native heap."

in the Ddms.cfg file (can not find it, use everything search) Last add a row Native=true and save. Ddms.cfg is located in the C:\Users\xxx\.android directory.

Select the package name of the app you want in the device, then press the snapshot button to observe the usage of the native heap, then execute the script repeatedly, then observe, and you'll find what you need.


(5) The problem of memory leaks can be complex, but sometimes these techniques are more important. So the second (5) Dot, additional command tips are provided to help find additional situations where memory leaks are a problem.

1. ADB shell Dumpsys activity queries the activity stack (once encountered, that is, activity has accumulated, resulting in memory overflow oom)

2. adb shell Dumpsys cpuinfo

3. adb Shell Dumpsys Battery

If you have some other methods in this area, then you may wish to share with us!


Reprint Please specify: http://blog.csdn.net/ljphhj/article/details/44966023

Reproduced in the Fat Tiger CSDN



Android Memory leak analysis brief idea

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.