1page.title=Tracking Memory Allocations 2@jd:body 3 4<p>Writing efficient mobile applications is not always straightforward. In 5particular, Android applications rely on automatic memory management handled by 6Dalvik's garbage collector, which can sometimes cause performance issues if you 7are not careful with memory allocations.</p> 8 9<p>In a performance-sensitive code path, such as the layout or drawing method of 10a view or the logic code of a game, any allocation comes at a price. After too 11many allocations, the garbage collector will kick in and stop your application 12to let it free some memory. Most of the time, garbage collections happen fast 13enough for you not to notice. However, if a collection happens while you are 14scrolling through a list of items or while you are trying to defeat a foe in a 15game, you may suddenly see a drop in performance/responsiveness of the 16application. It's not unusual for a garbage collection to take 100 to 200 ms. 17For comparison, a smooth animation needs to draw each frame in 16 to 33 ms. If 18the animation is suddenly interrupted for 10 frames, you can be certain that 19your users will notice.</p> 20 21<p>Most of the time, garbage collection occurs because of tons of small, 22short-lived objects and some garbage collectors, like generational garbage 23collectors, can optimize the collection of these objects so that the application 24does not get interrupted too often. The Android garbage collector is 25unfortunately not able to perform such optimizations and the creation of 26short-lived objects in performance critical code paths is thus very costly for 27your application.</p> 28 29<p>To help you avoid frequent garbage collections, the Android SDK ships with a 30very useful tool called <em>allocation tracker</em>. This tool is part of DDMS, 31which you must have already used for debugging purposes. To start using the 32allocation tracker, you must first launch the standalone version of DDMS, which 33can be found in the <code>tools/</code> directory of the SDK. The version of 34DDMS included in the Eclipse plugin does not offer you ability to use the 35allocation tracker yet.</p> 36 37<p>Once DDMS is running, simply select your application process and then click 38the <em>Allocation Tracker</em> tab. In the new view, click <em>Start 39Tracking</em> and then use your application to make it execute the code paths 40you want to analyze. When you are ready, click <em>Get Allocations</em>. A list 41of allocated objects will be shown in the first table. By clicking on a line you 42can see, in the second table, the stack trace that led to the allocation. Not 43only you will know what type of object was allocated, but also in which thread, 44in which class, in which file and at which line. The following screenshot shows 45the allocations performed by <a 46href="http://code.google.com/p/shelves">Shelves</a> while scrolling a 47ListView.</p> 48 49<a href="images/ddms_allocation_trackerl.png"> 50 51<img style="cursor:hand;width: 320px; height: 250px;" src="images/ddms_allocation_tracker.png" border="0" alt="" /> 52</a> 53 54<p>Even though it is not necessary — and sometimes not possible — to 55remove all allocations for your performance critical code paths. the allocation 56tracker will help you identify important issues in your code. For instance, a 57common mistake I have seen in many applications is to create a new 58<code>Paint</code> object on every draw. Moving the paint into an instance field 59is a simple fix that helps performance a lot. I highly encourage you to peruse 60the <a href="http://source.android.com/">Android source code</a> to see how we 61reduce allocations in performance-critical code paths. You will also thus 62discover the APIs Android provide to help you reuse objects.</p> 63