Before introducing how to debug, you must first know whether the current system has been reflected or not? The following describes some common recognition methods:
- CAT/proc/meminfo // roughly check the types of the current memory and the size of each type
- PS // check the actual situation of each process
- Top // see which processes are most used by the previous notebook
- Dumpsys meminfo // Android-specific commands with more information
Method 1 memory Analyzer:
This method must be used with eclipse because memory analyzer (MAT) is a plug-in of Eclipse.
- The first step is the installation math. You can download the memoryanalyzer-1.0.1.201008091353.zip file to the plug-ins, or select help --> install new software... --> Add on eclipse, and then click name
Logging into memory analyzer, entering a http://download.eclipse.org/mat/1.0/update-site/ in location logging, as shown in logging: Press
OK. Then you need to select the Security plug-in to complete the security program.
- The android program you want to debug as usual, then, check whether/data/MISC/in the shell has the authority to limit the access, because we will dump the file to this project later. In addition, you can set his permission to 777 simply:
adb shellchmod 777 /data/misc/
- Next, let's take a long trip to the place where you think the leak will be generated, and then use the kill command to send the signal SIGUSR1 (-10) process (you can view the process ID through PS), and you need to debug "com. test. debug "this process:
ps...PID UID VSZ Stat Command100 1000 255255 S com.test.debug... kill -10 100
- You can confirm through logcat that your process has not received signal:
logcat &
If the following response message is displayed, the message is received:
I/dalvikvm( 236): threadid=3: reacting to signal 10I/dalvikvm( 236): SIGUSR1 forcing GC and HPROF dumpI/dalvikvm( 236): hprof: dumping VM heap to "/data/misc/heap-dump-tm1291081439-pid100.hprof-hptemp".I/dalvikvm( 236): hprof: dumping heap strings to "/data/misc/heap-dump-tm1291081439-pid100.hprof".I/dalvikvm( 236): hprof: heap dump completed, temp file removedD/dalvikvm( 236): GC_HPROF_DUMP_HEAP freed 585 objects / 60000 bytes in 10595ms
Check/data/MISC/object should be able to see the heap-dump-tm1291081439-pid100.hprof:
ls /data/misc/...heap-dump-tm1291081439-pid100.hprof...
- Open Shell
exit
The ADB pull captured the case.
adb pull /data/misc/heap-dump-tm1291081439-pid100.hprof
- Through hprof-Conv, the attack cases captured by zookeeper are converted into the format recognized by memory Analyzer:
hprof-conv heap-dump-tm1291081439-pid100.hprof debug.hprof
- Start eclipse, select window --> open perspective --> other..., and then select memory analyer.
- Click file --> open heap dump..., and then select Debug. hprof ). Next, a dialog box appears, asking you what kind of warning you want to generate, as shown in the following figure: At this time, our goal is to improve performance.
Memory leak, so it is natural to choose leak suspects report. After you press finish, a warning will be generated, telling you something suspicious. Is it great!
Method 2 libc_debug.so:
Basically, this method converts libc. So into libc_debug.so and uses libc_debug.so to provide the Multi-Dimensional Memory Function to check whether the memory is improperly used.
- Convert/system/lib/libc. So to/system/lib/libc_debug.so
- Re-installing the system ADB shell reboot
- ADB shell setprop libc. Debug. malloc 1 "(or 5, or 10 for slightly different behaviors)
- Add "Native = true" to your ~ /. Android/ddms. cfg pending
- Activate the stand-alone version of ddms. Then you should see the "Native Heap" tab. After this tab appears, you can use native memory.
Method 3 C/C ++ debuuger:Use C/C ++ debugger to debug the memory leak of C/C ++ debugging. The following example uses dmalloc to debug the JNI native library.
- First go to The dmalloc official website http://www.dmalloc.com under your source code.
- Cross-region upload dmalloc. I personally prefer to use Android ndk to upload images, however, before that, you must generate the original makefile and then generate Android according to the makefile. mk
- Batch row configure?
1 |
. /configure
--prefix= /arm-linux/
-- enable -cxx -- enable -threads |
Here,/ARM-Linux/is the path designated by the kernel. We just want to test makefile.
- Test makefile and convert it to Android. mk.
- Click "ndk-build" to compile mongodmalloc.
- Because we want to debug the native library of JNI, we cannot specify the debug options through the general method of setting environment changes :?
12 |
DMALLOC_OPTIONS=debug=0x4e48503,inter=100,log= /sdcard/dmalloc .%p export
DMALLOC_OPTIONS |
You must specify the debug option at the program import point. For example, JNI can be used as the import point. In addition, when the website is opened, it must also be related :?
1234567891011 |
#include "dmalloc.h" JNIEXPORT jint JNICALL JNI_OnLoad ( ) { dmalloc_debug_setup ( "debug=0x4e48503,inter=100,log=/sdcard/dmalloc.%p" ); } JNIEXPORT void
JNICALL JNI_OnUnLoad ( ) { dmalloc_shutdown (); } |
Then, use dmalloc_mark () and dmalloc_log_changed () to mark the region to be deleted. In the following example, we use a JNI function to intentionally create a memory leak :?
123456789101112131415161718192021 |
unsigned long
mark; JNIEXPORT void
JNICALL JNI_com_example_debug_Test ( JNIEnv* env, jobject thisArg ) { // Mark the start of debug mark = dmalloc_mark(); // Leak memory 1000 bytes. char * leak = ( char *)
malloc (1000); // log unfreed pointers that have been added to // the heap since mark dmalloc_log_changed ( mark, 1,
// log unfreed pointers 0,
// do not log freed pointers 1
// log each pnt otherwise summary ); } |
Once this function is called, dmalloc.1000 is generated under/sdcard/category (the Sub-role name is the PID of this process ), in the future, we will tell you that the line has generated a memory leak.
?
123456 |
948436802: 1: Dmalloc version
'5.5.2' from
'http://dmalloc.com/' 948436802: 1: flags = 0x4e48503, logfile
'logfile' 948436802: 1: interval = 100, addr = 0, seen
# = 0, limit = 0 948436802: 1: starting
time = 948436802 948436802: 1: process pid = 1000 ... |
Post: http://yindingtsai.blogspot.com/2010/11/howto-debug-memory-leak-in-android.html