Track memory allocations on Android

Despite the impressive hardware of the first Android phones (T-Mobile G1 and ADP1) writing efficient mobile applications is not always straightforward. Android applications rely on automatic memory management handled by Dalvik’s garbage collector which can sometimes cause performance issues if you are not careful with memory allocations.

In a performance sensitive code path, like the layout or drawing method of a view or the logic code of a game, any allocation comes at a price. After too many allocations, the garbage collector will kick in and stop your application to let it free some memory. Most of the time, garbage collections happen fast enough for you not to notice. However, if a collection happens while you are scrolling through a list of items or while you are trying to defeat a foe in a game, you may suddenly see a drop in performance/responsiveness of the application. It’s not unusual for a garbage collection to take 100 to 200 ms. For comparison, a smooth animation needs to draw each frame in 16 to 33 ms. If the animation is suddenly interrupted for 10 frames, you can be certain that your users will notice.

Most of the time, garbage collection occurs because of tons of small, short-lived objects and some garbage collectors, like generational garbage collectors, can optimize the collection of these objects so that the application does not get interrupted too often. The Android garbage collector is unfortunately not able to perform such optimizations and the creation of short-lived objects in performance critical code paths is thus very costly for your application.

To help you avoid frequent garbage collections, the Android SDK ships with a very useful tool called allocation tracker. This tool is part of DDMS, which you must have already used for debugging purposes. To start using the allocation tracker, you must first launch the standalone version of DDMS, which can be found in the tools/ directory of the SDK. The version of DDMS included in the Eclipse plugin does not offer you ability to use the allocation tracker yet.

Once DDMS is running, simply select your application process and then click the Allocation Tracker tab. In the new view, click Start Tracking and then use your application to make it execute the code paths you want to analyze. When you are ready, click Get Allocations. A list of allocated objects will be shown in the first table. By clicking on a line you can see, in the second table, the stack trace that led to the allocation. Not only you will know what type of object was allocated, but also in which thread, in which class, in which file and at which line. The following screenshot shows the allocations performed by Shelves while scrolling a ListView.

Android's allocation tracker

Even though it is not necessary, or sometimes not possible, to remove all allocations for your performance critical code paths. the allocation tracker will help you identify important issues in your code. For instance, a common mistake I have seen in many applications is to create a new Paint object on every draw. Moving the paint into an instance field is a simple fix that helps performance a lot. I highly encourage you to peruse the Android source code to see how we reduce allocations in performance critical code paths. You will also thus discover the APIs Android provide to help you reuse objects.

Happy allocations hunting!

13 Responses to “Track memory allocations on Android”

  1. Mariano Kamp says:

    Hi Romain,

    thanks for the post. Very helpful. I was living with the assumption that the Eclipse version of DDMS is complete. Good to know that there is more.


  2. Tim says:

    This just serves to highlight how bad a language choice Java is for phones. Give me C++ any day!

  3. Peter Meijer says:

    Thanks Romain! Very useful. I too had been working only with the Eclipse version of DDMS, not knowing that the one in the Android SDK tools folder would offer more.

  4. Romain Guy says:

    Tim, it has nothing to do with Java. If Android had a generational GC this would matter a lot less.

  5. OK, im not sure if this is the right place to post it, but I’ll just go ahead…

    I tried this but for something seems to be wrong with my DDMS …

    See this screenshot:

  6. neni says:

    sedih gue kok android jlek gitu sich mending gue cari alternatif lain. ya bule2 jelek

  7. Mile says:

    Great post Romain, very informative for an android beginner.
    Could you please give me some examples as to where to look for performance critical code in the android platform?
    Also, i would appreciate a few hints on the APIs you mention, used for object reuse?

    Thanks :)

  8. Alan says:

    Running DDM on win xp. When clicking on allocation tracker tab — it shows no start tracking button even with process highlighted. It shows disabled “?” button and disabled “Get Alocations” button.

    What am I missing?

  9. D.Larson says:

    Wasn’t exactly what I had been looking for but started reading the first few lines and it was interesting so ended up reading the complete post, thanks.

  10. Damir says:

    Answer to Alan:
    I had same problem. I was using DDMS tool 1.5 and trying to connect to emulator 1.6. I fixed problem by running DDMS 1.5 with emulator 1.5. It is backward compatibility issue.

  11. Nice to see you blogging about this good topic.