Andy McFadden | aa33602 | 2009-04-27 13:19:51 -0700 | [diff] [blame] | 1 | <html> |
| 2 | <head> |
| 3 | <title>Dalvik Heap Profiling</title> |
| 4 | </head> |
| 5 | |
| 6 | <body> |
| 7 | <h1>Dalvik Heap Profiling</h1> |
| 8 | |
| 9 | <p> |
| 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. |
| 14 | |
| 15 | |
| 16 | <h2>Getting the data</h2> |
| 17 | <p> |
| 18 | The first step is to cause the VM to dump its status, and then pull the hprof |
| 19 | data off. The exact manner for doing so has changed over time. |
| 20 | </p><p> |
| 21 | There is a <code>runhat</code> shell function, added by |
| 22 | <code>build/envsetup.sh</code>, that partially automates these steps. The |
| 23 | function changes in each release to accommodate newer behavior, so you have |
| 24 | to be careful that you don't use the wrong version. |
| 25 | </p><p> |
| 26 | |
| 27 | <h3>Early releases (1.0/1.1)</h3> |
| 28 | <p> |
| 29 | You can only generate heap data on the emulator or a device with root |
| 30 | access, because of the way the dump is initiated and where the output |
| 31 | files go. |
| 32 | </p><p> |
| 33 | Get a command shell on the device: |
| 34 | <blockquote><pre> |
| 35 | $ adb shell |
| 36 | </pre></blockquote> |
| 37 | </p><p> |
| 38 | You can verify that you're running as root with the <code>id</code> command. |
| 39 | The response should look like <code>uid=0(root) gid=0(root)</code>. If not, |
| 40 | type <code>su</code> and try again. If <code>su</code> fails, you're out |
| 41 | of luck. |
| 42 | |
| 43 | </p><p> |
| 44 | Next, ensure the target directory exists: |
| 45 | <blockquote><pre> |
| 46 | # mkdir /data/misc |
| 47 | # chmod 777 /data/misc |
| 48 | </pre></blockquote> |
| 49 | |
| 50 | </p><p> |
| 51 | Use <code>ps</code> or DDMS to determine the process ID of your application, |
| 52 | then send a <code>SIGUSR1</code> to the target process: |
| 53 | |
| 54 | <blockquote><pre> |
| 55 | # kill -10 <pid> |
| 56 | </pre></blockquote> |
| 57 | |
| 58 | </p><p> |
| 59 | The signal causes a GC, followed by the heap dump (to be completely |
| 60 | accurate, they actually happen concurrently, but the results in the heap |
| 61 | dump reflect the post-GC state). This can take a couple of seconds, |
| 62 | so you have to watch for the GC log message to know when it's complete. |
| 63 | </p><p> |
| 64 | Next: |
| 65 | |
| 66 | <blockquote><pre> |
| 67 | # ls /data/misc/heap-dump* |
| 68 | # exit |
| 69 | </pre></blockquote> |
| 70 | |
| 71 | </p><p> |
| 72 | Use <code>ls</code> to check the file names, then <code>exit</code> to quit |
| 73 | the device command shell. |
| 74 | |
| 75 | </p><p> |
| 76 | You should see two output files, named |
| 77 | <code>/data/misc/heap-dump-BLAH-BLAH.hprof</code> and |
| 78 | <code>.hprof-head</code>, where BLAH is a runtime-generated value |
| 79 | that ensures the filename is unique. Pull them off of the device and |
| 80 | remove the device-side copy: |
| 81 | |
| 82 | <blockquote><pre> |
| 83 | $ adb pull /data/misc/heap-dump-BLAH-BLAH.hprof tail.hprof |
| 84 | $ adb pull /data/misc/heap-dump-BLAH-BLAH.hprof-head head.hprof |
| 85 | $ adb shell rm /data/misc/heap-dump-BLAH-BLAH.hprof /data/misc/heap-dump-BLAH-BLAH.hprof-head |
| 86 | </pre></blockquote> |
| 87 | |
| 88 | </p><p> |
| 89 | Merge them together and remove the intermediates: |
| 90 | |
| 91 | <blockquote><pre> |
| 92 | $ cat head.hprof tail.hprof > dump.hprof |
| 93 | $ rm head.hprof tail.hprof |
| 94 | </pre></blockquote> |
| 95 | |
| 96 | </p><p> |
| 97 | You now have the hprof dump in <code>dump.hprof</code>. |
Andy McFadden | aa33602 | 2009-04-27 13:19:51 -0700 | [diff] [blame] | 98 | </p><p> |
| 99 | |
| 100 | |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 101 | <h3>Android 1.5 ("Cupcake")</h3> |
Andy McFadden | aa33602 | 2009-04-27 13:19:51 -0700 | [diff] [blame] | 102 | <p> |
| 103 | Some steps were taken to make this simpler. Notably, the two output |
| 104 | files are now combined for you, and a new API call was added that allows |
| 105 | a program to write the dump at will to a specific file. If you're not |
| 106 | using the API call, you still need to be on an emulator or running as root. |
| 107 | (For some builds, you can use <code>adb root</code> to restart the adb |
| 108 | daemon as root.) |
| 109 | </p><p> |
| 110 | The basic procedure is the same as for 1.0/1.1, but only one file will |
| 111 | appear in <code>/data/misc</code> (no <code>-head</code>), and upon |
| 112 | completion you will see a log message that says "hprof: heap dump completed". |
| 113 | It looks like this in the log: |
| 114 | |
| 115 | <blockquote><pre> |
| 116 | I/dalvikvm( 289): threadid=7: reacting to signal 10 |
| 117 | I/dalvikvm( 289): SIGUSR1 forcing GC and HPROF dump |
| 118 | I/dalvikvm( 289): hprof: dumping VM heap to "/data/misc/heap-dump-tm1240861355-pid289.hprof-hptemp". |
| 119 | I/dalvikvm( 289): hprof: dumping heap strings to "/data/misc/heap-dump-tm1240861355-pid289.hprof". |
| 120 | I/dalvikvm( 289): hprof: heap dump completed, temp file removed |
| 121 | </pre></blockquote> |
| 122 | |
| 123 | </p><p> |
| 124 | Summary: as above, use <code>mkdir</code> and <code>chmod</code> |
| 125 | to ensure the directory exists and is writable by your application. |
| 126 | Send the <code>SIGUSR1</code> or use the API call to initiate a dump. |
| 127 | Use <code>adb pull <dump-file></code> and <code>adb shell rm |
| 128 | <dump-file></code> to retrieve the file and remove it from the |
| 129 | device. The concatenation step is not needed. |
| 130 | |
| 131 | </p><p> |
| 132 | The new API is in the <code>android.os.Debug</code> class: |
| 133 | <blockquote><pre> |
| 134 | public static void dumpHprofData(String fileName) throws IOException |
| 135 | </pre></blockquote> |
| 136 | When called, the VM will go through the same series of steps (GC and |
| 137 | generate a .hprof file), but the output will be written to a file of |
| 138 | your choice, e.g. <code>/sdcard/myapp.hprof</code>. Because you're |
| 139 | initiating the action from within the app, and can write the file to |
| 140 | removable storage or the app's private data area, you can do this on a |
| 141 | device without root access. |
| 142 | |
| 143 | |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 144 | <h3>Android 1.6 ("Donut")</h3> |
| 145 | <p> |
Andy McFadden | 0998df5 | 2009-11-20 13:42:29 -0800 | [diff] [blame] | 146 | No real change to the way profiling works. |
| 147 | However, 1.6 introduced the <code>WRITE_EXTERNAL_STORAGE</code> |
| 148 | permission, which is required to write data to the SD card. If you're |
| 149 | accustomed to writing profile data to <code>/sdcard</code>, you will |
| 150 | need to enable the permission in your application's manifest. |
| 151 | |
| 152 | |
| 153 | <h3>Android 2.0 ("Eclair")</h3> |
| 154 | <p> |
| 155 | In 2.0, features were added that allow DDMS to request a heap dump on |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 156 | demand, and automatically pull the result across. Select your application |
Andy McFadden | 0998df5 | 2009-11-20 13:42:29 -0800 | [diff] [blame] | 157 | and click the "dump HPROF file" button in the top left. This always |
| 158 | writes files to the SD card, so |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 159 | you must have a card inserted and the permission enabled in your application. |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 160 | |
| 161 | |
Andy McFadden | aa33602 | 2009-04-27 13:19:51 -0700 | [diff] [blame] | 162 | <h2>Examining the data</h2> |
| 163 | <p> |
| 164 | The data file format was augmented slightly from the common hprof format, |
| 165 | and due to licensing restrictions the modified <code>hat</code> tool cannot |
| 166 | be distributed. A conversion tool, <code>hprof-conv</code>, can be used |
| 167 | to strip the Android-specific portions from the output. This tool was |
| 168 | first included in 1.5, but will work with older versions of Android. |
| 169 | </p><p> |
| 170 | The converted output should work with any hprof data analyzer, including |
| 171 | <code>jhat</code>, which is available for free in the Sun JDK, and |
| 172 | Eclipse MAT. |
| 173 | |
| 174 | <!-- say something about how to track down common problems, interesting |
| 175 | things to look for, ...? --> |
| 176 | |
Andy McFadden | 842e20c | 2009-04-30 14:12:27 -0700 | [diff] [blame] | 177 | </p><p> |
| 178 | <address>Copyright © 2009 The Android Open Source Project</address> |
| 179 | |
Andy McFadden | aa33602 | 2009-04-27 13:19:51 -0700 | [diff] [blame] | 180 | </body> |
| 181 | </html> |