Android development practices: detects App memory usage and leakage

Source: Internet
Author: User

Android applications developed some time ago suddenly fell down after running for about half an hour every time. It was very inexplicable and did not know where the problem occurred. Later, I checked them step by step, the error is found at the JNI layer. The memory allocated by a frequently called function is forgotten to be released, resulting in Memory leakage.


This problem makes me understand that the Android program is based on the Java language and has a powerful garbage collection mechanism, so there is no need to worry about the memory. In fact, the Android program must be especially careful with your memory, after all, because mobile phones are no better than PCs, the memory is extremely limited. When the memory is insufficient, the system will Kill your program at any time. As I have learned, memory problems are prone to the following:


1) when operating Bitmap objects, be sure to note that recycle is not used.


2) when accessing the database, remember to close the cursor.


3) Code involving the JNI layer. Because the JNI layer is written in C/C ++, you need to manage the memory allocation/Recycle by yourself, so be careful.


In Android development, how can we effectively detect memory usage and Memory leakage?


Three methods are introduced here:


1. Program Log information


During garbage collection, the program prints a Log information logcat window. For example:


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


Note that the "3571 K/9991K" value in this message represents the heap size used by the program, it indicates that your program has a memory leak.


2. Heap information of DDMS


The Eclipse development environment also provides a more intuitive way to view the Heap information of an App. The operation is as follows:


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140101/0023121S5-0.png "title =" 16:45:46 screen .png "alt =" 16.02508.png"/>


1) connect to the mobile phone and run the program. Assume It is the com. ticktick. test program.


2) Click the "DDMS" button and select the program com. ticktick. test in the Device window on the left)


3) Click Update Heap in the toolbar of the Device window ),


4) Click the Cause GC button in the window on the right to obtain the Heap information of the current program.


Similarly, as the program runs, the Heap size after multiple clicks. If you only increase or decrease, it also indicates that your program has memory leakage.


3. Run the adb command to view memory information.


In fact, the most comprehensive and simplest way is to use the adb command to view the program's memory usage and Memory leakage. Open the command line window. The adb command format is as follows:


adb shell dumpsys meminfo <package_name>


Package_name can also be changed to the pid of the program. pid can be searched through adb shell top | grep app_name. In the command line window, run the preceding command to get my com. ticktick. the memory of the test program is as follows:


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140101/002312E00-1.png "title =" 2.png" alt = "165221492.png"/>


Here we get a lot of information, focusing on the following fields:


1) Native/Dalvik Heap Information


Specifically, in the first and second rows above, it gives the memory allocation at the JNI and Java layers. If you find that this value keeps increasing, memory leakage may occur in the program.


2) Total PSS Information


This value is the actual memory occupied by your application. With this information, you can easily identify which programs on your mobile phone occupy a large amount of memory.


4. Summary


The usage of memory in Android development and the detection of Memory leakage are briefly introduced here. Basically, the above three methods can be used to locate the memory leakage problem. Bitmap is usually used, when programming the database and the JNI layer C/C ++, pay attention to it. In addition, if you want to learn more about the content in this article, you can refer to the two articles officially provided by Google, which have a more detailed description of "Investigating Your RAM Usage". "Managing Your App Memory", any questions or unclear places, welcome to leave a message or send a letter to the lujun.hust@gmail.com to communicate.


This article from the "three shadows" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1346785

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.