Saturday, 5 November 2011

Android - track down memory leaks

My current Android application project is starting to make sense. Unfortunately it crasches after a few levels of playing due to java.lang.OutOfMemoryError. Up to that point I hadn't put much thinking into the memory model of Android applications and simply consumed memory without hesitations. I've now been forced to rewrite some critical parts of the application and i thought I'll write a few words to remember the most useful tools I came across.

First of all, Android apps have small heaps. And with different sizes, it's up to the vendor of the device to decide. Here's a few numbers I came across:

  • G1 = 16 Mb
  • Droid = 24 Mb
  • Nexus One = 32 Mb
  • Xoom = 48 Mb
  • GalaxyTab = 64 Mb

So you see that allocated heaps are far from using the entire RAM of the devices since no application should be able to clog the system. The natural approach to solving a memory problem would be to increase the heap but that is not so easy. If you have a rooted phone you may edit

and set the heap size via

Or, if you're running on a tablet (3.x) Android version there is a manifest setting to ask for a large heap

<application android:label="@string/app_name" android:hardwareAccelerated="true" android:largeHeap="true" android:debuggable="true">
but that is no guarantee and you will instead be punished with longer GC cycle times.

On the other hand, changing the VM heap size in your emulator is easy, and could be a good thing in order to verify that your app works on devices with smaller heaps. To do that, fire up your Android SDK and AVD Manager and click edit on your virtual device. Under hardware, there is a setting Max VM application heap size.

So the conclusion is that you have to live with small heaps and limited memory. How to get an estimate of  your consumed memory and how much there is available then?
Run your application in the emulator or connect your real device via USB and use the Android Debug Bridge (adb). It's located in your Android SDK tools folder.

To dump memory info for all your running applications use
$>adb shell dumpsys meminfo

or for your specific application

$>adb shell dumpsys meminfo

Applications Memory Usage (kB):
Uptime: 8979886 Realtime: 8979886

** MEMINFO in pid 1073 [] **
                    native   dalvik    other    total
            size:    24648    10119      N/A    34767
       allocated:    10869     7335      N/A    18204
            free:        2     2784      N/A     2786
           (Pss):     2857     8568     9385    20810
  (shared dirty):     1508     4092     2556     8156
    (priv dirty):     2656     6020     7732    16408

           Views:        0        ViewRoots:        0
     AppContexts:        0       Activities:        0
          Assets:        2    AssetManagers:        2
   Local Binders:        6    Proxy Binders:       10
Death Recipients:        0
 OpenSSL Sockets:        0

            heap:        0       memoryUsed:        0
pageCacheOverflo:        0  largestMemAlloc:        0

To understand this table we must know that you have a managed heap, dalvik, and a native heap. For example some graphics are stored in native heap. But important, it is the sum of these heaps that can not exceed the VM heap size. so you can't fool the runtime by putting more stuff in either native or managed heap. So to me, the most important numbers are the number under dalvik and total above. The dalvik heap is the managed VM heap and the native numbers are memory allocated by native libraries (malloc).
You'll probably see these numbers fluctating each time you run the command, that is because objects are allocated by the runtime all the time but GCs are not run particularly often. So, in order to know that you really have garbage collected all unused objects you must either wait for the Android debug log in logcat to say something like
GC_FOR_MALLOC or GC_EXTERNAL_MALLOC or similar to that which indicates that the GC has been invoked. Still, this does not mean that all unused memory has been released since the inner workings of the GC might not have done a complete sweep.

You can of course ask for a GC programmatically by System.gc();
But that is never a good option. You should have trust in the VM to garbage collect for you. If you for example want to allocate a large memory chunk the gc will be invoked if necessary.

You can force a gc using the Dalvik Debug Monitor (DDMS). Either start it from Eclipse or from the ddms tool in the Android SDK installation folders.

If you can't see your process right away, go to menu Actions and Reset adb. After that you can turn on heap updates via the green icon Show heap updates. To force a GC, click on Cause GC.

If you wish to monitor the memory usage programmatically there are a few APIs you can use.

ActivityManager.getMemoryInfo() can be used to get an idea of how the memory situation is for the whole Android system. If running low on the gauges you can expect background processes to be killed off soon.

To start inspecting your process in particular use the Debug APIs There's an excellent explanation of the data you can retrieve from this here

For example, to see how much memory is allocated in the native heap, use:

So back to DDMS. This tool can also create heap dumps which are particulary useful when tracing down memory leaks. To dump the heap, click on the icon Dump HPROF file. There are many tools for analyzing heap dumps but the one I'm most familiar is the Eclipse Memory Analyzer (MAT). Download it from

MAT can't handle the DDMS heap dumps right away, but there is a converter tool in the Android SDK.
So simply run this command.

C:\Temp\hprof>hprof-conv rawdump.hprof converteddump.hprof

Then you can open the converted heap dump in MAT. An important concept here is retained size. The retained size of an object found in the heap is how much memory could be freed if this object could be garbage collected. That includes the object itself, but also child objects which no other objects outside of the retained set has references to.

MAT gives you an overview of where your memory is allocated and has some good tooling on finding suspicious allocations that could be memory leaks.

So to find my memory leak, I used the dominator tree tab which sorts the allocated objects by retained heap
and I soon discovered that the GLRendered object held far too many references to a large 512x512 texture.

The tool becomes even more valuable when the leaking objects are small but many. The dominator tree tell you right away that you have a single object holding a much larger retained heap than you would expect it to.

If you want to learn more, check out this speech by Patrick Dubroy on Android memory management from Google IO 2011 where he explains the Android memory model in more detail.


  1. I still dont understand how you found out about the texture reference.

    Another question i got is that MAT tells me about 4 possible memory leaks but i dont understand anything about them.

  2. Good point, so what I did was make a heap dump after say 1 minute of running the app. This for reference. Then I ran the app for some further amount of time and made a new heap dump. Opening the two dumps beside eachother it is clear that the later had allocated 3 mb more memory as texture objects. After that you must of course know your program well enough to recognise this as a bug.

  3. Giulio Piancastelli20 July 2012 at 16:02

    So I assume that if the dominators are system classes (e.g. Resources or Bitmap in an application using huge images) that's the sign there is no leak, and images needs to be scaled down, resampled, or whatever it takes to reduce their size, since that's the real cuprit.

  4. Yep very helpful i'm having an issue where i run out of memory after at least 20minutes of a "chat" channel which has images refreshing every 10seconds. I'll try as you did and take a dump early...let the program run (refreshing away...) for 10+ minutes and see what comparisons i can make between the two in MAT.

    BTW i've tried inspecting using DDMS Dump heap > Threads, Heap and allocation tracker and couldn't find anything. I assume MAT is easier to find problems??


  5. It's marvelous I didn't know about all this until the moment I saw this entry of yours. In addition to that I want to ask you a question which is extremely intriguing for me. Do you own any helpful data about how to protect your personal intellectual property from being used without your awareness about it?

  6. Thanks, everything worked out setting it up. Found link to this on StackOverflow.

  7. That's interesting! Can you please share more about it? Thank you.

    Android Application Developer India & Android Application Development Company

  8. Android is a versatile working framework that has been remarkably great due to its staggering characteristics. The majority of the Pdas in the business sector run on Android OS. Since the accomplishment of Android, parcel of Android Improvement Companies has prodded up all over. We are additionally putting great rationale to create all the more in this field.
    Develop Android Apps // Mobile
    Application Development
    // Android Application Development

  9. Hi your blog is good but it can be better. No hard feelings! This is just my personal opinion. You have to stand out among millions of online writers who are blogging like crazy, uploading articles every micro-second. There are wonderful tips for freelancers who gain by getting `online noticed’ – in other words `web visible’. Check out,Ebook cover brochure design websites. They offer the best hints on great blog writing.

  10. This comment has been removed by the author.

  11. last part really helped me. I was able to look at dominator tree, but was not able to figure out how to zero down on object which is causing it. Now I know that expanding it till the end will reveal the object name.
    Though I have one question, when you said "a single object holding a much larger retained heap than you would expect it to.", so in that image, how do you figure out where the leak was happening in your code just by looking at 'int[2342342]'? This is still a mystery to me.

  12. Good Job. Very informative blog. It's really helpful for Android Application Development company for making new apps.

  13. Android is a versatile working framework that has been remarkably great due to its staggering characteristics. The majority of the Pdas in the business sector run on Android OS. Android Application Development

  14. Thanks, Currently Android applications are running fast then others for Tablets. It is also to know about track down leaks of memory. Tanks for sharing such kind of details.
    Android Tablet Development

    Keep sharing..

  15. Interesting blog, helped to clarify many things, definitely this will be a useful article. Good work, excepting these kinds of more useful blogs and articles.
    Mobile app development Company

  16. Hey, very nice site. I came across this on Google, and I am stoked that I did. I will definitely be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just taking in as much info as I can at the moment. Thanks for sharing.

    Dai Software

  17. Hey, very nice site. I came across this on Google, and I am stoked that I did. I will definitely be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just taking in as much info as I can at the moment. Thanks for sharing.

    Mobile Application Development

  18. What does it mean by memory leak? Could you please explain more
    App Developer Dubai

  19. Thank you so much for sharing nice information and i want to read other posts also

    apps development companies in Ahmedabad
    iot companies Ahmedabad

  20. This comment has been removed by the author.

  21. I really enjoyed this posting in which you share a valuable post. Thanks for sharing it. web design company in Ahmadabad.

  22. Thank you for bestowing this information which will enlighten many of us. It’s really helpful for me and the one who wants to know more about mobile app development company.

  23. thank you for the valuable post.


  24. Your post is amazed me. Your article is very much informative. You always share such a wonderful articlewhich helps us to gain knowledge .thanks for sharing such a wonderful article, It will be deinitely helpful and fruitful article. hope we will always gain from your article.

  25. Those students are eagerly waiting for SSC CGL 2019 Notification. Staff Selection Commission will announce SSC CGL notification in the month of October 2019

  26. You have shared really helpful tutorials. I like it. Thanks for sharing such a great stuff. Web Solution Winner Blog

  27. National Testing Agency is going to conduct the JEE Main 2020 Examination .the exam is conducted twice in a year in this article we are sharing all the important details for jee main 2020 click here for all the details

  28. thank you for sharing this useful information

    Cpa offers

  29. You have Top companies listed well in India. I will strongly accommodate the one company. The Company Name is Freelance To India Which provide the top quality services in Website and Mobile Application Development.

  30. Thanks, Excellent Services related company information you have shared. Web Solution Winner is The World's Most Successful Blog. We write about Technology, Business, Entertainment, Lifestyle, SEO, Travel, USA, UK, Canada. Guest Blogging Solution.

  31. Software Development Company We specialize in Blockchain development, Artificial Intelligence, DevOps, Mobile App development, Web App development and all your customised online solutions. Get best impression at online by our services, we are familiar for cost effectiveness, quality, delivery and support.

  32. Thanks for sharing such a great information with us.Keep on updating us with these types of blogs.
    professional web design company in chennai
    top 10 web designing companies in chennai

  33. You have provided a useful information about Memory Monitor of apps. We can use the Memory Monitor to detect memory leaks through the your provided steps. Anyone can also write for us about this topic.

  34. Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you once agianMarriage certificate in delhi
    Marriage certificate in ghaziabad
    Marriage registration in gurgaon
    Marriage registration in noida
    special marriage act
    Marriage certificate online
    Marriage certificate in mumbai
    Marriage certificate in faridabad
    Marriage certificate in bangalore
    Marriage certificate in hyderabad thanks once again to all.

  35. This comment has been removed by the author.

  36. Nice blog! Full of informative ideas. Thank you, keep sharing.
    web design company in chennai

  37. Clarity of concepts in Pass4sure Checkpoint dumps is really praiseworthy. I have used this dumps material for the preparation of my Checkpoint exam. Dumpspass4sure helped me in all possible ways to get succeeded in my dream certification. Pass4sure Checkpoint dumps pdf helped me to ace my exam.

  38. Awesome Post,

    Thanks a lot for sharing the great piece of the content with us. I would surely refer to the steps to find an ideal Casino Software Provider.

    Main part of the company is in a perfect position alongside the USA CasinoPokerGuru is a leading outsourcing casino game development company which specializes in software making, HTML5, Java development, web application development, blockchain development.

    We customizable software solutions with a new idea according to the clients need and his budget.
    Casino Game Solution

    Keep it works and share with us your latest post.

    Thanks again!


  39. It is very useful information at my studies time, i really very impressed very well articles and worth information, i can remember more days that articles.

    catering services in chennai
    tasty catering services in chennai
    best catering services in chennai
    top catering services in chennai
    veg Catering services in chennai

  40. Aran’s traditional milk is pure A2 milk, Nattu Kozhi Muttai Chennai, Organic Milk Chennai, A2 Milk Chennai, Cow Milk Chennai, Naatu Maatu Paal Chennai Chennai hand-milked in a traditional way from healthy native Indian breeds and reaches your doorstep.

    Milking Process
    The milking is done from indigenous cows by using hands. No machines are used in order to ensure no harm is done to the cows

    Packing Methods
    As soon as milking is done, the milk is filtered and packed in the FSSAI certified place with hairnets and gloves on this packing is done into the 50 microns wrappers which are not reactive to the food items. Again, no machines are used for packing to contribute to the environment, as they consume more water and power.

    Milk Delivery
    As soon as packing and quality check are done, the milk packets are collected and brought for delivery.

  41. Fantastic Blog! It really gives proper collaboration to understand the main concept step by step.

    Employee Survey Application Development

  42. Awesome, what a wonderful post I have never seen such a lovely post like this. So I want to tell you that we design a website that performs just well. With the right design and codes, we design the website that brings maximum leads and conversions. Attract and delight your customers with a range of Web Design Services - from Dynamic to Responsive to Creative to Mobile-first and much more. Let’s connect and discuss the right design for you.

  43. BMIT – BSc Medical Imaging Technology – Here is the details about Best BSc Medical Imaging Technology Colleges In Bangalore. If you are looking to study in Bangalore, the below link will redirect to a page that will show the best BMIT colleges in Bangalore
    BSc Medical Imaging Technology Colleges In Bangalore

  44. Hi,

    Thanks a lot for sharing this valuable and useful content with us. Being a reputational company in bulletproof suits providers in 2019. We have been on an epic ride and paved our way to becoming the Best in designing the suits in US. Aspetto known as an essential part of our voyage and we will continue to serve them better with the same loyalty and ace-quality of products. Aspetto is proud to be America's First Bullet Resistant Clothing Line. Shop for men's suits, body armor, and ballistic clothing.

    We always understand your needs and provide you exactly what you want.

    Thanks again for sharing valuable content with us.

    Thanks & Regards!
    Aspetto SEO Team

  45. With the help of creative designing team TSS advertising company provides different branding and marketing strategies in advertising industry...


    Yang Merupakan Agen Bandarq, Domino 99, Dan Bandar Poker Online Terpercaya di asia hadir untuk anda semua dengan permainan permainan menarik dan bonus menarik untuk anda semua

    Bonus yang diberikan NagaQQ :
    * Bonus rollingan 0.5%,setiap senin di bagikannya
    * Bonus Refferal 10% + 10%,seumur hidup
    * Bonus Jackpot, yang dapat anda dapatkan dengan mudah
    * Minimal Depo 15.000
    * Minimal WD 20.000

    Memegang Gelar atau title sebagai QQ Online Terbaik di masanya

    Games Yang di Hadirkan NagaQQ :
    * Poker Online
    * BandarQ
    * Domino99
    * Bandar Poker
    * Bandar66
    * Sakong
    * Capsa Susun
    * AduQ
    * Perang Bacarrat (New Game)

    Tersedia Deposit Via pulsa :
    Telkomsel & XL

    Info Lebih lanjut Kunjungi :
    Website : NAGAQQ
    Facebook : NagaQQ Official
    Kontakk : Info NagaQQ
    linktree : Agen Judi Online
    WHATSAPP : +855977509035
    Line : Cs_nagaQQ
    TELEGRAM : +855967014811

    agen bandarq terbaik
    Winner NagaQQ
    Daftar NagaQQ
    Agen Poker Online

  47. Really Nice Post, Thanks for sharing & keep up the good work.
    Oflox Is The Best Website Designer In Dehradun

  48. Very interesting blog on android memory leaks and will definitely help a newbie like me to gain insights on how to track down the memory leak. Please keep sharing more android app development articles. Thank you!

    Mobile Application Development Companies in Chennai

    Mobile App Development Company in Chennai

    Android Application Development Company in Dubai

    iOS application development company in Dubai

  49. Nice blog...
    Thanks for sharing such a informative post with us. It will really help us.

  50. Appsinvo is the Mobile App Development Company. With the help of our team passion and hard work we have come a long way and many milestones are still to achieve in the coming days. We serve clients ranging from startups, SMEs to large enterprises. We build the applications as per the clients’ requirements but we give them a different touch by using the trendy designs, latest technologies and agile methodologies
    Mobile App development company in Asia
    Top Mobile App Development Company
    Top Mobile App Development Company in India
    Top Mobile App Development Company in Noida
    Mobile App Development Company in Delhi
    Top Mobile App Development Companies in Australia
    Top Mobile App Development Company in Qatar
    Top Mobile App Development Company in kuwait
    Top Mobile App Development Companies in Sydney
    Mobile App Development Company in Europe
    Mobile App Development Company in Dubai

  51. thanks for your post.We know how that feels as our clients always find themselves to be the top rankers. It's really easy when you consult that best SEO company in Chennai.

    Best SEO Services in Chennai | digital marketing agencies in chennai |Best seo company in chennai | digital marketing consultants in chennai | Website designers in chennai

  52. If you need to change your mobile number in Gmail account(link is external), then don’t get panic at all as you can now simply apply settings to your account and solve your all Gmail issues.

    More queries search by Gmail users-

    How do I Call Google Customer Service

    How do I Get my Gmail Account Back

    How do I Recover my Account Password

    Can I Call Gmail Support

    How do I Change my Gmail Password

    How do I Contact Gmail Support Center

  53. If you want to 100% real instagram followers. India based users always trying to get real instagram followers and other social media services Buy instagram followers India

  54. If you are looking for a phone number to contact cash app support center, the you have landed on the right page. In the article below, we will tell you about Cash App and how to get support services for your accounts

  55. I was looking for extraordinary online journals and I saw your blog. I extremely happy in the wake of seeing your blog content. A debt of gratitude is in order for sharing your blog. Best website development agency uk

  56. it is amazing, like to learn more about it,waiting for next blog

    zplus cyber secure technology pvt. Ltd.

  57. CrownQQ Agen DominoQQ BandarQ dan Domino99 Online Terbesar

    Yuk Buruan ikutan bermain di website CrownQQ
    Sekarang CROWNQQ Memiliki Game terbaru Dan Ternama loh...

    9 permainan :
    => Poker
    => Bandar Poker
    => Domino99
    => BandarQ
    => AduQ
    => Sakong
    => Capsa Susun
    => Bandar 66
    => Perang Baccarat (NEW GAME)

    => Bonus Refferal 20%
    => Bonus Turn Over 0,5%
    => Minimal Depo 20.000
    => Minimal WD 20.000
    => 100% Member Asli
    => Pelayanan DP & WD 24 jam
    => Livechat Kami 24 Jam Online
    => Bisa Dimainkan Di Hp Android0619679319
    => Di Layani Dengan 5 Bank Terbaik
    => 1 User ID 9 Permainan Menarik

    Ayo gabung sekarang juga hanya dengan
    mengklick Agen BandarQ

    Link Resmi CrownQQ:

    Info Lebih lanjut Kunjungi :
    Website : CrownQQ
    Daftar CrownQQ : Poker Online
    WHATSAPP : +855882357563
    Line : CS CROWNQQ
    Facebook : CrownQQ Official
    Kemenangan CrownQQ : Agen BandarQ

  58. thanks for sharing great article with us.nice article blog.River Group of Salon and spa, T.Nagar, provide a wide range of spa treatments, like body massage, scrub, wrap and beauty parlour services. We ensure unique care and quality service.

    massage in T.Nagar | body massage T.Nagar | massage spa in T.Nagar | body massage center in T.Nagar | massage centre in chennai | body massage in chennai | massage spa in chennai | body massage centre in chennai | full body massage in T.Nagar

  59. Great drive to the memory leaks when it comes to android. Just want to find if this happened with iOS and Symbian operating systems also.

  60. thanks for sharing with great article blog.keep posting with us.We at Fuel digital marketing give you the best E commerce website development services in Chennai. Which will give you and your customers one-stop solution and best-in-class services

    e commerce website development services in chennai | best woocommerce development company | best ecommerce website design company in chennai | ecommerce website designing company in chennai | opencart development company in chennai | digital marketing company in chennai | website designing company in chennai | digital marketing agency in chennai

  61. Thanks for sharing great article with us. Such a nice article blog. I likes your post. Thanks for posting this out! Amazing post and a great read!

    Free DoFollow Travel Blog Commenting Sites

  62. Why is my canon printer is offline mac? If you are looking for the solution to resolve this error then get connected with our expert team. Who are working at day as well as night to help you. For more information you can check out our website canon printer offline. Our experts are always ready to help you.

  63. Are you facing the kindle fire error code 13? So without wasting your time, get connected with our team. Our technicians for sure give immediate solutions to you to overcome this issue. For more information visit our website Ebook Helpline.

  64. Such a great blog post. They are nice information. Thanks for sharing us. Rajasthan Budget Tours

  65. Thanks for this blog, I really enjoyed reading your post.

  66. Struggling with QuickBooks Enterprise Support Phone Number ? Fix it by dialling us at 1-855-533-6333. The error usually arrives due to improper installation of QuickBooks upgrading.

    Click here :- with QuickBooks Enterprise Support Phone Number ? Fix it by dialling us at 1-855-533-6333. The error usually arrives due to improper installation of QuickBooks upgrading.

    Click here :-

  67. Struggling with QuickBooks Enterprise Support Phone Number ? Fix it by dialling us at 1-855-533-6333. The error usually arrives due to improper installation of QuickBooks upgrading.

    Click here :- with QuickBooks Enterprise Support Phone Number ? Fix it by dialling us at 1-855-533-6333. The error usually arrives due to improper installation of QuickBooks upgrading.

    Click here :-

  68. Struggling with Phone number for QuickBooks ? Fix it by dialling us at 1-855-533-6333. The error usually arrives due to improper installation of QuickBooks upgrading.

  69. Nice Blog !
    QuickBooks Payroll Error 30159 is one of the complex payroll errors that generally takes place because of improper file setup in the Operating System of your PC.