3 <title>Dalvik Heap Profiling</title>
7 <h1>Dalvik Heap Profiling</h1>
10 The Dalvik virtual machine can produce a complete dump of the contents
11 of the virtual heap. This is very useful for debugging memory usage
12 and looking for memory leaks. Getting at the information can be tricky,
13 but has become easier in recent releases.
15 In what follows, the version number refers to the software release
16 running on the phone. To take advantage of the DDMS integration, you will
17 also need a sufficiently recent version of DDMS.
20 <h2>Getting the data</h2>
22 The first step is to cause the VM to dump its status, and then pull the hprof
23 data off. The exact manner for doing so has changed over time.
25 There is a <code>runhat</code> shell function, added by
26 <code>build/envsetup.sh</code>, that partially automates these steps. The
27 function changes in each release to accommodate newer behavior, so you have
28 to be careful that you don't use the wrong version.
31 <h3>Early releases (1.0/1.1)</h3>
33 You can only generate heap data on the emulator or a device with root
34 access, because of the way the dump is initiated and where the output
37 Get a command shell on the device:
42 You can verify that you're running as root with the <code>id</code> command.
43 The response should look like <code>uid=0(root) gid=0(root)</code>. If not,
44 type <code>su</code> and try again. If <code>su</code> fails, you're out
48 Next, ensure the target directory exists:
51 # chmod 777 /data/misc
55 Use <code>ps</code> or DDMS to determine the process ID of your application,
56 then send a <code>SIGUSR1</code> to the target process:
59 # kill -10 <pid>
63 The signal causes a GC, followed by the heap dump (to be completely
64 accurate, they actually happen concurrently, but the results in the heap
65 dump reflect the post-GC state). This can take a couple of seconds,
66 so you have to watch for the GC log message to know when it's complete.
71 # ls /data/misc/heap-dump*
76 Use <code>ls</code> to check the file names, then <code>exit</code> to quit
77 the device command shell.
80 You should see two output files, named
81 <code>/data/misc/heap-dump-BLAH-BLAH.hprof</code> and
82 <code>.hprof-head</code>, where BLAH is a runtime-generated value
83 that ensures the filename is unique. Pull them off of the device and
84 remove the device-side copy:
87 $ adb pull /data/misc/heap-dump-BLAH-BLAH.hprof tail.hprof
88 $ adb pull /data/misc/heap-dump-BLAH-BLAH.hprof-head head.hprof
89 $ adb shell rm /data/misc/heap-dump-BLAH-BLAH.hprof /data/misc/heap-dump-BLAH-BLAH.hprof-head
93 Merge them together and remove the intermediates:
96 $ cat head.hprof tail.hprof > dump.hprof
97 $ rm head.hprof tail.hprof
101 You now have the hprof dump in <code>dump.hprof</code>.
105 <h3>Android 1.5 ("Cupcake")</h3>
107 Some steps were taken to make this simpler. Notably, the two output
108 files are now combined for you, and a new API call was added that allows
109 a program to write the dump at will to a specific file. If you're not
110 using the API call, you still need to be on an emulator or running as root.
111 (For some builds, you can use <code>adb root</code> to restart the adb
114 The basic procedure is the same as for 1.0/1.1, but only one file will
115 appear in <code>/data/misc</code> (no <code>-head</code>), and upon
116 completion you will see a log message that says "hprof: heap dump completed".
117 It looks like this in the log:
120 I/dalvikvm( 289): threadid=7: reacting to signal 10
121 I/dalvikvm( 289): SIGUSR1 forcing GC and HPROF dump
122 I/dalvikvm( 289): hprof: dumping VM heap to "/data/misc/heap-dump-tm1240861355-pid289.hprof-hptemp".
123 I/dalvikvm( 289): hprof: dumping heap strings to "/data/misc/heap-dump-tm1240861355-pid289.hprof".
124 I/dalvikvm( 289): hprof: heap dump completed, temp file removed
128 Summary: as above, use <code>mkdir</code> and <code>chmod</code>
129 to ensure the directory exists and is writable by your application.
130 Send the <code>SIGUSR1</code> or use the API call to initiate a dump.
131 Use <code>adb pull <dump-file></code> and <code>adb shell rm
132 <dump-file></code> to retrieve the file and remove it from the
133 device. The concatenation step is not needed.
136 The new API is in the <code>android.os.Debug</code> class:
138 public static void dumpHprofData(String fileName) throws IOException
140 When called, the VM will go through the same series of steps (GC and
141 generate a .hprof file), but the output will be written to a file of
142 your choice, e.g. <code>/sdcard/myapp.hprof</code>. Because you're
143 initiating the action from within the app, and can write the file to
144 removable storage or the app's private data area, you can do this on a
145 device without root access.
148 <h3>Android 1.6 ("Donut")</h3>
150 No real change to the way profiling works.
151 However, 1.6 introduced the <code>WRITE_EXTERNAL_STORAGE</code>
152 permission, which is required to write data to the SD card. If you're
153 accustomed to writing profile data to <code>/sdcard</code>, you will
154 need to enable the permission in your application's manifest.
158 <h3>Android 2.0 ("Eclair")</h3>
160 In 2.0, features were added that allow DDMS to request a heap dump on
161 demand, and automatically pull the result across. Select your application
162 and click the "dump HPROF file" button in the top left. This always
163 writes files to the SD card, so
164 you must have a card inserted and the permission enabled in your application.
168 <h3>Android 2.2 ("Froyo")</h3>
170 DDMS heap dump requests are now streamed directly out of the VM, removing
171 the external storage requirement.
174 <h3>Android 2.3 ("Gingerbread")</h3>
176 The <code>kill -10</code> (<code>SIGUSR1</code>) method of generating heap
177 dumps has been removed from the VM.
180 <h2>Examining the data</h2>
182 The data file format was augmented slightly from the common hprof format,
183 and due to licensing restrictions the modified <code>hat</code> tool cannot
184 be distributed. A conversion tool, <code>hprof-conv</code>, can be used
185 to strip the Android-specific portions from the output. This tool was
186 first included in 1.5, but will work with older versions of Android.
188 The converted output should work with any hprof data analyzer, including
189 <code>jhat</code>, which is available for free in the Sun JDK, and
192 <!-- say something about how to track down common problems, interesting
193 things to look for, ...? -->
196 <address>Copyright © 2009 The Android Open Source Project</address>