Dalvik heap profiling

Source: Internet
Author: User

Https://android.git.kernel.org /? P = platform/Dalvik. Git; A = blob_plain; F = docs/heap-profiling.html; HB = head

 

Dalvik heap profiling

The Dalvik virtual machine can produce a complete dump of the contents of the virtual heap. this is very useful for debugging memory usage and looking for memory leaks. getting at the information can be tricky, but has become easier in recent releases.

In what follows, the version number refers to the software release running on the phone. To take advantage of the ddms integration, you will also need a sufficiently recent version of ddms.

Getting the data

The first step is to cause the VM to dump its status, and then pull the hprof data off. The exact manner for doing so has changed over time.

There isrunhatShell function, addedbuild/envsetup.sh, That partially automates these steps. The function changes in each release to accommodate newer behavior, so you have to be careful that you don't use the wrong version.

 

Early releases (1.0/1.1)

You can only generate heap data on the emulator or a device with root access, because of the way the dump is initiated and where the output files go.

Get a command shell on the device:

$ adb shell

 

You can verify that you're re running as root withidCommand. The response showould look likeuid=0(root) gid=0(root). If not, TypesuAnd try again. IfsuFails, you're out of luck.

Next, ensure the target directory exists:

# mkdir /data/misc# chmod 777 /data/misc

 

UsepsOr ddms to determine the process ID of your application, then sendSIGUSR1To the target process:

# kill -10 <pid>

 

The signal causes a GC, followed by the heap dump (to be completely accurate, they actually happen concurrently, but the results in the heap dump reflect the post-GC State ). this can take a couple of seconds, so you have to watch for the GC Log message to know when it's complete.

Next:

# ls /data/misc/heap-dump*# exit

 

UselsTo check the file names, thenexitTo quit the device command shell.

You shoshould see two output files, named/data/misc/heap-dump-BLAH-BLAH.hprofAnd.hprof-head, Where blah is a runtime-generated value that ensures the filename is unique. Pull them off of the device and remove the device-side copy:

$ adb pull /data/misc/heap-dump-BLAH-BLAH.hprof tail.hprof$ adb pull /data/misc/heap-dump-BLAH-BLAH.hprof-head head.hprof$ adb shell rm /data/misc/heap-dump-BLAH-BLAH.hprof /data/misc/heap-dump-BLAH-BLAH.hprof-head

 

Merge them together and remove the intermediates:

$ cat head.hprof tail.hprof > dump.hprof$ rm head.hprof tail.hprof

 

You now have the hprof dump indump.hprof.

 

Android 1.5 ("cupcake ")

Some steps were taken to make this simpler. notably, the two output files are now combined for you, and a new API call was added that allows a program to write the dump at will to a specific file. if you're not using the API call, you still need to be on an emulator or running as root. (For some builds, you can useadb rootTo restart the ADB daemon as root .)

The basic procedure is the same as for 1.0/1.1, but only one file will appear in/data/misc(No-head), And upon completion you will see a log message that says "hprof: heap dump completed". It looks like this in the log:

I/dalvikvm(  289): threadid=7: reacting to signal 10I/dalvikvm(  289): SIGUSR1 forcing GC and HPROF dumpI/dalvikvm(  289): hprof: dumping VM heap to "/data/misc/heap-dump-tm1240861355-pid289.hprof-hptemp".I/dalvikvm(  289): hprof: dumping heap strings to "/data/misc/heap-dump-tm1240861355-pid289.hprof".I/dalvikvm(  289): hprof: heap dump completed, temp file removed

 

Summary: As abve, usemkdirAndchmodTo ensure the directory exists and is writable by your application. SendSIGUSR1Or use the API call to initiate a dump. Useadb pull <dump-file>Andadb shell rm <dump-file>To retrieve the file and remove it from the device. The concatenation step is not needed.

The new API is inandroid.os.DebugClass:

public static void dumpHprofData(String fileName) throws IOException

When called, the VM will go through the same series of steps (GC and generate a. hprof file), but the output will be written to a file of your choice, e.g./sdcard/myapp.hprof. Because you're initiating the action from within the app, and can write the file to removable storage or the app's private data area, you can do this on a device without root access.

Android 1.6 ("Donut ")

No real change to the way profiling works. However, 1.6 introducedWRITE_EXTERNAL_STORAGEPermission, which is required to write data to the SD card. If you're accustomed to writing profile data/sdcard, You will need to enable the permission in your application's manifest.

Android 2.0 ("eclair ")

In 2.0, features were added that allow ddms to request a heap dump on demand, and automatically pull the result into ss. select your application and click the "Dump hprof file" button in the top left. this always writes files to the SD card, so you must have a card inserted and the permission enabled in your application.

Android 2.2 ("Froyo ")

Ddms heap dump requests are now streamed directly out of the VM, removing the external storage requirement.

Examining the data

The data file format was augmented slightly from the common hprof format, and due to licensing restrictions the modifiedhatTool cannot be distributed. A conversion tool,hprof-conv, Can be used to strip the Android-specific portions from the output. This tool was first defined ded in 1.5, but will work with older versions of Android.

The converted output shocould work with any hprof data analyzer, includingjhat, Which is available for free in the Sun JDK, and eclipse mat.

 

Copyright 2009 the android open source project

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.