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

/system/build.props
and set the heap size via
dalvik.vm.heapsize=24m


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 se.noren.android.othello


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


** MEMINFO in pid 1073 [se.noren.android.othello] **
                    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


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


 SQL
            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 http://developer.android.com/intl/de/reference/android/os/Debug.html#getMemoryInfo(android.os.Debug.MemoryInfo. There's an excellent explanation of the data you can retrieve from this here http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android

For example, to see how much memory is allocated in the native heap, use:
Debug.getNativeHeapAllocatedSize()

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 http://www.eclipse.org/mat/.

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.






122 comments:

  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.

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete
  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??

    Thanks

    ReplyDelete
  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?

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

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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 www.zopaudios.com,Ebook cover brochure design websites. They offer the best hints on great blog writing.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. 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.

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

    ReplyDelete
  12. 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

    ReplyDelete
  13. 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..

    ReplyDelete
  14. 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

    ReplyDelete
  15. What does it mean by memory leak? Could you please explain more
    Farhan,
    App Developer Dubai

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

    apps development companies in Ahmedabad
    iot companies Ahmedabad

    ReplyDelete
  17. This comment has been removed by the author.

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

    ReplyDelete
  19. thank you for the valuable post.

    ReplyDelete


  20. 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.
    Thanks
    DedicatedHosting4u.com

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

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

    ReplyDelete
  23. 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

    ReplyDelete
  24. 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.

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. 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.

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



    Employee Survey Application Development

    ReplyDelete
  28. 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.

    ReplyDelete
  29. 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

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

    ReplyDelete
  31. 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

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

    ReplyDelete
  33. 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

    ReplyDelete
  34. 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

    ReplyDelete
  35. 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

    ReplyDelete
  36. 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

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

    zplus cyber secure technology pvt. Ltd.

    ReplyDelete
  38. 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

    ReplyDelete
  39. 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.

    ReplyDelete
  40. 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


    ReplyDelete
  41. 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

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

    ReplyDelete
  43. 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 :- https://tinyurl.com/y2lafr4pStruggling 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 :- https://tinyurl.com/y2lafr4p

    ReplyDelete
  44. 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 :- https://tinyurl.com/y2lafr4pStruggling 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 :- https://tinyurl.com/y2lafr4p

    ReplyDelete
  45. 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.

    ReplyDelete
  46. software testing company in India
    software testing company in Hyderabad
    Nice and informative blog.
    I hope u add some more information about this blog.
    keep sharing.

    ReplyDelete
  47. Excellent post! Your post is very useful and I felt quite interesting reading it. Expecting more post like this. Thanks for posting such a good post.

    ReplyDelete
  48. Nice post and valuable knowledge . Check our website too which has good information.

    Our B2B app is used for managing applications and all the activities related to B2B is handled very easily. We are India based b2b developer who specialize in developing android applications and have been producing high quality development apps in India for a long span of time.

    ReplyDelete
  49. You have brought up very fantastic points, thank you for the post
    Food Delivery App Development

    ReplyDelete
  50. This is a good post. This post gives truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. Thank you so much
    connected devices services
    bockchain development


    ReplyDelete
  51. Nice Information. Thanks for sharing. Muby Technologies , is a leading pioneer in the field of image editing services.
    We take pride in establishing ourselves as the most reliable partner for your image retouching and photo editing services.
    Reach us

    ReplyDelete
  52. Hello, Excellent Blog with loads of information on this blog, Thanks for sharing with us. We ColourMoon Technologies specialists in making
    Mobile App Development Companies in Vizag
    Healthcare App Development in Vizag
    ecommerce app development company in Vizag
    Mobile app development companies in India
    Game Design Companies in Hyderabad
    Website Designers Agency in Kukatapally
    ERP Development Company in Visakapatnam
    Best Graphic Designers in Hyderabad

    https://thecolourmoon.com/

    ReplyDelete
  53. Great information!!! Thanks for your wonderful informative blog.
    Village Talkies a top-quality professional corporate video production company in Bangalore and also best explainer video company in Bangalore & animation video makers in Bangalore, Chennai, India & Maryland, Baltimore, USA provides Corporate & Brand films, Promotional, Marketing videos & Training videos, Product demo videos, Employee videos, Product video explainers, eLearning videos, 2d Animation, 3d Animation, Motion Graphics, Whiteboard Explainer videos Client Testimonial Videos, Video Presentation and more for all start-ups, industries, and corporate companies. From scripting to corporate video production services, explainer & 3d, 2d animation video production , our solutions are customized to your budget, timeline, and to meet the company goals and objectives.
    As a best video production company in Bangalore, we produce quality and creative videos to our clients.

    ReplyDelete
  54. This comment has been removed by the author.

    ReplyDelete
  55. Nice to be visiting your blog again, it has been months for me. Well, this article that i've been waited for so long. I need this article to complete my assignment in college, and it has the same topic as your article. Thanks, great share. learn more about java developer.

    ReplyDelete
  56. Very Interesting post. We know how that feels as our clients always find themselves to be the top rankers.

    Mobile App Development Company in Chennai
    EV App Development Company in Chennai

    ReplyDelete
  57. If you are searching for someone for solve your love problem, then we assure you that we are always available for you. You can reach us at any time. Click on this link : Best Astrologer in Brisbane

    ReplyDelete
  58. This is so amazing article i got useful information from your blog . thanks for sharing such as information with us . you can also benefit QuickBooks Accounting software at

    quickbooks support phone number

    ReplyDelete
  59. Thank you for sharing it with us, Keep posting.
    Branding Agency Dubai

    ReplyDelete
  60. Its Great Article thanks for sharing this type wonderful information please update in future again and again .if you are looking accounting service you can get by this at

    quickbooks support phone number

    ReplyDelete
  61. Your blog was absolutely fantastic! Large amount of great information which is often attractive some and the other way.Thanks.
    online food delivery app development

    ReplyDelete
  62. It is high time to provide a new dimension to your home service business by partnering to create an on-demand home service application that can help you reach a maximum number of targeted audiences and land you into the zone of profitability.
    VISIT-https://lilacinfotech.com/what-we-do/home-service-on-demand-app-development-india

    ReplyDelete
  63. A digital transformation consulting firm with dedicated development team can help you figure out how to convey the new business goals and why they were necessary. And leaders need to communicate the change and how the transformation will benefit each individual.

    ReplyDelete
  64. Thanks for sharing I like it and also useful for me
    batman

    ReplyDelete
  65. When suffering from errors in QuickBooks, a professional consultant can help you. our experts can help troubleshoot the issues at QuickBooks Customer Support Phone Number - Minnesota+1(866) 898-9639.

    ReplyDelete
  66. Good work, Interesting blog, helped to clarify many things, definitely this will be a useful article. Please keep sharing more Mobile android app development articles. Thank you!
    Mobile Website Development Company in Amritsar

    ReplyDelete
  67. Hey there
    Nice post, Thanks for sharing it

    Try - Zinzo - Online Grocery Shopping app to get groceries at lowest prices

    ReplyDelete
  68. We are providing you the best app development services which will convert your next app idea into a productive app suitable for Android or iOS platforms. Our skilled team has updated knowledge about the latest versions of mobile designing platforms.


    https://lilacinfotech.com/what-we-do/app-development

    ReplyDelete
  69. Very awesome post! I like that and very interesting content colourist logo

    ReplyDelete
  70. Excellent information with unique content and it is very awesome to know about the information based on website.. Write For Us Tech

    ReplyDelete
  71. I want to appreciate this bog post because it has given me the best knowledge and you can find the professional Website development Dubai at exclusive offers.

    ReplyDelete
  72. Your blog is very interesting. Your level of thinking is good and the clarity of writing is excellent. I enjoyed reading this post. I am also a blogger, You can visit our services here Android App Development Services
    You can also contact here, if you are looking forward to Hire Android App Developers or best Mobile App Development Company

    ReplyDelete
  73. Hey! I am Gregory Gladiya. I am here from the technical team to assist you in resolving the Epson printer issues. For further information on on driver install, click here: Epson ET 2760 Driver. Here you will be able to find a solution to resolve issues that are faced with Epson printers of any model.

    ReplyDelete
  74. buy-fake-canadian-dollars-online
    buy-fake-australian-dollars-online
    buy-original-passports-online
    buy-original-id-cards-online
    buy-original-id-cards-online
    buy-fake-usd-online
    buy-fake-british-pounds-online
    buy-undetectable-euro-banknotes-online


    buy-aed-bills-20-online

    buy-aed-bills-50-online
    buy-aed-bills-200-online
    buy-aed-bills-100-online

    ‎buy-inr-20-online
    buy-inr-50-online

    buy-nzd-50-bills
    buy-nzd-100-bills-online
    buy-australian-20-bills-online
    buy-australian-100-bills-online

    buy-cad-10-bills-online
    buy-cad-20-bills-online
    buy-cad-100-bills-online

    buy-gbp-5-bills-online-
    buy-gbp-50-bills-online

    buy-euro-50-bills-online
    buy-euro-100-billsbuy-euro-500-bills-for-sale
    buy-usd-20-bills-online
    buy-usd-50-bills-online
    buy-usd100-bills-online


    ReplyDelete
  75. Nice blog post. I really enjoyed reading. Thanks for sharing. Keep posting.

    ReplyDelete
  76. Your blog is very interesting. Your level of thinking is good and the clarity of writing is excellent.
    seo

    ReplyDelete
  77. Android stands out as a highly adaptable operating system, renowned for its remarkable features. A significant portion of the mobile devices in the market operates on the Android OS. The success of Android has spurred the emergence of numerous Android development companies, and we are actively contributing to further advancements in this field.

    ReplyDelete
  78. Tracking down memory leaks in Android can be a crucial task to ensure your app's performance and user experience remain top-notch. With Awkits
    web developers expertise, you can trust that they have the skills and tools necessary to identify and resolve these issues efficiently. Don't let memory leaks hold your app back; partner with Awkits to keep your Android app running smoothly and impressing users

    ReplyDelete