Android Development Practice: detecting memory footprint and leaks in apps

Source: Internet
Author: User

Source: http://www.linuxidc.com/Linux/2014-03/97563.htm

The development of Android applications in the previous period, each time after running for about half an hour or so suddenly hung up, it is inexplicable, do not know where the problem, and then step by step to troubleshoot, found that the problem in the JNI layer, a frequently called function allocated memory forgotten to release, resulting in memory leaks.

This problem makes me understand, do not think that the Android program is based on the Java language, there is a powerful garbage collection mechanism, there is no need to worry about memory problems, in fact, the Android program should be particularly careful of your memory, because after all, the phone is not more than the PC, memory is extremely limited, when the memory is not enough, The system will kill your program at any time. What I've learned at the moment is that the memory problems are prone to the following:

(1) The Operation bitmap object, must pay attention, when does not use the time recycle

(2) To access the database, you must remember to close the cursor

(3) The code involved in the JNI layer, because the JNI layer is written in C + +, you need to manage the allocation/recycling of memory, so be careful.

So what is the effective way to detect memory usage and memory leaks in Android development?

Here are three ways to focus:

1. Log information of the program

When the program is garbage collected, a log message (Logcat window) is printed, for example:

D/DALVIKVM (9050): Gc_concurrent freed 2049K, 65% free 3571k/9991k, external 4703k/5261k, paused 2ms+2ms

Note the "3571k/9991k" value in this message, which represents the size of the heap used by the program, and if the value has been increasing and never decreasing, then there is a memory leak in your program.

2. Heap information for DDMS

The Eclipse development environment also provides a more intuitive way to view the heap information for an app as follows:

(1) Connect the phone, run the program, assuming the Com.ticktick.test program

(2) Click the Ddms button to select the program you want to detect in the device window on the left (com.ticktick.test)

(3) Click on the second icon in the Device window toolbar (Update Heap),

(4) Click the Cause GC button on the right window to get the heap information of the current program.

Similarly, as the program runs, multiple clicks to get the heap size, if only increase, it also indicates that your program has a memory leak.

3. ADB command to view memory information

In fact, the most comprehensive and simplest way is to use the ADB command to view the memory footprint and memory leaks of the program, open a command-line window, the format of the ADB command is as follows:

adb shell Dumpsys meminfo <package_name>

Among them, package_name can also be replaced by program Pid,pid can be through the adb shell top | grep app_name to find, run the above command in the command-line window, get the memory situation of my Com.ticktick.test program as follows:

There is a lot of information here, focusing on the following fields:

(1) Heap information for Native/dalvik

Specifically in the first and second lines above, it gives the JNI layer and the Java layer of memory allocation, if it is found that the value has been increasing, the representative of the program may have a memory leak.

(2) PSS information for total

This value is the amount of memory your app really occupies, and with this information, you can easily identify which programs in your phone are more memory-heavy.

4. Summary

About the use of memory in the development of Android and memory leak detection simply introduced here, basically in the above three ways can be located in the memory leak problem, usually in the use of bitmap, database and JNI layer C/D + + programming, pay attention to a little bit on the line. In addition, if you want to learn more about the details of the article, you can refer to the two official Google articles, they have a more detailed discussion of "investigating Your RAM Usage", "Managing Your App Memory", If you have any questions or unclear, please feel free to leave a message or a letter [email protected] to communicate.

Android Development Practice: detecting memory footprint and leaks in apps

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.