1page.title=Heap Viewer Walkthrough 2meta.tags="android, performance, profiling, tools, memory, memoryleaks, memoryheap" 3page.tags="android", "performance", "profiling", "tools", "memory", "memoryleaks", "memoryheap" 4page.metaDescription=Report in real-time how many objects of what types and sizes your application is allocating and find memory leaks. 5page.image=tools/performance/thumbnails/tools_heap_viewer.png 6page.article=true 7 8@jd:body 9 10<style> 11 .no-bullet { 12 list-style-type: none; 13 } 14 .padded { 15 padding-left: 10px; 16 } 17</style> 18 19<div id="tb" style="margin-left: 7px; margin-bottom: 5px;"> 20 21<h2>In this document</h2> 22<ul> 23 <li><a href="#WhatYouNeed">Prerequisites</a></li> 24 <li><a href="#WorkingWithHeapViewer">Working with Heap Viewer</a></li> 25</ul> 26 27<h2>You should also read</h2> 28<ul> 29 <li><a href="{@docRoot}tools/performance/memory-monitor/index.html"> 30 Memory Monitor Walkthrough</a></li> 31 <li><a href="{@docRoot}tools/performance/allocation-tracker/index.html"> 32 Allocation Tracker Walkthrough</a></li> 33 <li><a href="{@docRoot}tools/performance/comparison.html"> 34 Comparison: Memory Monitor, Heap Viewer, Allocation Tracker</a> 35 <li><a href="{@docRoot}tools/performance/traceview/index.html"> 36 Traceview Walkthrough</a></li> 37</ul> 38 39</div> 40 41 42 <p>This walkthrough shows the basic usage and workflow for the Heap Viewer tool. Heap Viewer 43 reports in real-time what <i>types</i> of objects your application has allocated, how many, and 44 their sizes on the heap.</p> 45 46 <p>What it's good for:</p> 47 48 <ul> 49 <li>Getting a sense of how your app allocates and frees memory.</li> 50 51 <li>Identifying memory leaks.</li> 52 </ul> 53 54 <h2 id="WhatYouNeed">Prerequisites</h2> 55 56 <ul> 57 <li>A mobile device running Android 5.0 or higher with <a href= 58 "http://developer.android.com/tools/device.html#developer-device-options">Developer Options</a> 59 enabled.</li> 60 61 <li>Application code. Use your own code, or the <a href= 62 "https://github.com/udacity/Sunshine-Version-2">Sunshine</a> sample app from the <a href= 63 "https://www.udacity.com/course/ud853">Android Fundamentals Udacity course</a>.</li> 64 </ul> 65 66 <h2 id="WorkingWithHeapViewer">Working with Heap Viewer</h2> 67 68 <ul class="no-bullet"> 69 <!-- this also hides bullet, by default outside box --> 70 <li><div style="overflow:hidden"> 71 <ol class="padded"> 72 73 <div class="figure" style=""> 74 <img src="{@docRoot}images/tools/performance/heap-viewer/gettingstarted_image01.png" 75 alt="" 76 width="400px" /> 77 <p class="img-caption"> 78 <strong>Figure 1. </strong>Starting Memory Monitor. 79 </p> 80 </div> 81 82 <li>Connect your mobile device to your computer.</li> 83 84 <li>Open your application in Android Studio, build the source, and run it on your device or 85 emulator.</li> 86 87 <li>Start the Android Device Monitor from Android Studio: <strong>Tools -> Android 88 -> Android Device Monitor</strong>. 89 90 <p>You can also start the <a href="{@docRoot}tools/studio/index.html#mem-cpu">Memory Monitor</a> 91 in Android Studio: Click the <em>Android</em> tab in the lower-left corner of the application 92 window. The CPU and Memory Monitor views appear.</p> </li> 93 94 </ol> 95</div></li> 96 97<li><div style="overflow:hidden"> 98<hr> 99 <ol class="padded" start="4"> 100 101 <div class="figure" style=""> 102 <img src="{@docRoot}images/tools/performance/heap-viewer/gettingstarted_image02.png" 103 alt="" 104 width="400px" /> 105 <p class="img-caption"> 106 <strong>Figure 2. </strong>Steps for Using Heap Viewer. 107 </p> 108 </div> 109 110 <li>In the device and application drop-downs, select the app you want to monitor.</li> 111 112</ol> 113</div></li> 114 115<li><div style="overflow:hidden"> 116<hr> 117 <ol class="padded" start="8"> 118 119 <div class="figure" style=""> 120 <img src="{@docRoot}images/tools/performance/heap-viewer/gettingstarted_image03.png" 121 alt="" 122 width="300px" /> 123 <p class="img-caption"> 124 <strong>Figure 3. </strong>Update Heap button. 125 </p> 126 </div> 127 128 <li>To save a heap dump, click the Dump Java Heap icon. The heap snapshot file 129 with the filename <code>Snapshot-yyyy.mm.dd-hh.mm.ss.hprof</code> appears in the 130 <em>Captures</em> tab.</li> 131 132</ol> 133</div></li> 134 135<li><div style="overflow:hidden"> 136<hr> 137 <ol class="padded" start="9"> 138 139 <div class="figure" style=""> 140 <img src="{@docRoot}images/tools/performance/heap-viewer/gettingstarted_image04.png" 141 alt="" 142 width="400px" /> 143 <p class="img-caption"> 144 <strong>Figure 4. </strong>Triggering GC (Garbage Collection). 145 </p> 146 </div> 147 148 <li>Note the <em>Heap updates will happen 149 after every GC for this client.</em> message.</li> 150 151 <li>Click the <strong>Initiate CG</strong> icon to 152 trigger a garbage collection event. 153 </li> 154</ol> 155</div></li> 156 157<li><div style="overflow:hidden"> 158<hr> 159 <ol class="padded" start="11"> 160 161 <div class="figure" style=""> 162 <img src="{@docRoot}images/tools/performance/heap-viewer/gettingstarted_image05.png" 163 alt="" 164 width="480px" /> 165 <p class="img-caption"> 166 <strong>Figure 5. </strong>Example Heap Viewer output. 167 </p> 168 </div> 169 170 <li>Double-click the heap snapshot file to open the heap viewer and see detailed information 171 about the current allocations on the heap.</li> 172 173 </ol> 174</div></li> 175 176 </ul> 177