16

I think my android app is leaking memory. I'm not absolutely sure that this is the problem though.

Every so often the app crashes when opening, and logcat shows an "out of memory" exception trying to load a bitmap image.

After crashing, I re-open the app and it works fine. Logcat shows lots of "gc"s and every once in a while the JIT table is resized upwards, never downwards until the app crashes with the out of memory error.

Does this sound like a memory leak? If so, how do I go about locating and closing the leak.

Here is my adb shell meminfo for my app.

** MEMINFO in pid 2691 [com.example.deepcliff] **
                    native   dalvik    other    total
            size:    23264     8839      N/A    32103
       allocated:    12503     3826      N/A    16329
            free:      168     5013      N/A     5181
           (Pss):     2512     1395    13815    17722
  (shared dirty):     2088     1844     5008     8940
    (priv dirty):     2412      224    11316    13952

 Objects
           Views:        0        ViewRoots:        0
     AppContexts:        0       Activities:        0
          Assets:        2    AssetManagers:        2
   Local Binders:       55    Proxy Binders:       13
Death Recipients:        1
 OpenSSL Sockets:        0

 SQL
               heap:      129         MEMORY_USED:      129
 PAGECACHE_OVERFLOW:        9         MALLOC_SIZE:       50

 DATABASES
      pgsz     dbsz   Lookaside(b)  Dbname
         1       14             10  webview.db
         1        6             18  webviewCache.db

 Asset Allocations
    zip:/data/app/com.example.deepcliff-2.apk:/resources.arsc: 17K
Arjun
  • 1,701
  • 4
  • 17
  • 25
  • First and foremost - I assume your app, on a freshly rebooted device - can load all the bitmaps it needs to when you work with it. Returning to it on the activity stack a few times will crash it? In that case you're most likely leaking memory by keeping references around to instances of your activities that have been destroyed. – Jens Nov 17 '11 at 21:07
  • 1
    1. Yes it normally works fine. It only crashes after I've used it for a while, closed it and opened it again. So if it is a leak of the manner you mentioned, where do I look to find references to instances of my activities. – Arjun Nov 17 '11 at 21:26
  • I'm curious if changing orientation (a lot) in certain activities affects anyhow? That would narrow down potential error cases. – Jarno Argillander Nov 17 '11 at 21:51
  • 1
    I don't allow changing orientation. It's always vertical. – Arjun Nov 17 '11 at 21:52
  • Have you checked if it crashes in the same frequency if you just open-close-open-close a lot? – Jarno Argillander Nov 17 '11 at 22:01
  • 1
    If possible, could we see the class, in which the "out of memory" exception occurs? Fresh eyes might spot something there. – Jarno Argillander Nov 17 '11 at 22:13
  • Why didn't use LeakCanary Lib to trace the memory leak, its easy to implement https://github.com/square/leakcanary – Islam Alshnawey Aug 15 '21 at 12:58

4 Answers4

33

Here are a couple of articles and posts, which probably help you to get on the right track:

Allocation tracker, which comes with Android SDK is very useful. Read Romain Guy's articles. It helped me to track down pretty nasty leaks. It also helps you to write better software. E.g. I learned to create less objects, use more StringBuilder, and cache a lot more:
What Android tools and methods work best to find memory/resource leaks?

Sometimes your app is just so messed up that you have to re-design it in the whole. Here are official, good hints for that (my favourite is the Avoid Creating Unnecessary Objects):
http://developer.android.com/guide/practices/design/performance.html


Here's an excellent article about attacking your memory issues:
http://ttlnews.blogspot.com/2010/01/attacking-memory-problems-on-android.html

Official article about avoiding memory leaks:
http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html

Read also this: tool to check memory leaks in android


Others already pointed about bitmaps. Here's an article describing the issue: http://zrgiu.com/blog/2011/01/android-bitmaps-and-out-of-memory-errors/

Community
  • 1
  • 1
Jarno Argillander
  • 5,885
  • 2
  • 31
  • 33
  • Does the code store any activities or drawables into static variables? Or is any of them stored outside the classes, which created them? It is really easy to leak tons of memory when one tries to optimize memory consumption or allocations in this way. E.g. store bitmaps into static variables when an activity is created. Then on every orientation change, the app would leak memory. – Jarno Argillander Nov 17 '11 at 21:45
  • I don't believe so, at least not intentionally. I'll go through my code again. Thanks for your links. – Arjun Nov 17 '11 at 21:50
  • Okay, so I have a user constantly switching between activities, meaning they may have switched 15 activities in 20 seconds. Could that be a cause of an out of memory error? What should I do to fix it? Thanks! – Ruchir Baronia Jan 05 '16 at 05:39
2

This is not a memory leak. Android devices just have a limited amount of memory and your bitmaps must just be too big. You need to find a way to reduce the size of your bitmaps. I really can't tell you much more because you haven't given us much to go on.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • Sorry, I'll edit my question. My largest image is a PNG about 600kb in size, which acts as a "loading" or splash screen. – Arjun Nov 17 '11 at 21:13
  • 3
    @Arjun This is a little orthogonal to the discussion, but you should never put a splash screen on your mobile app. It's probably going to really annoy your users. – Kurtis Nusbaum Nov 17 '11 at 21:18
  • I have various other small images for icons etc. If it was the image size, wouldn't this be easily reproducible? Right now I have to open the app, play around with it for a good few minutes, then close and open it to get the error. – Arjun Nov 17 '11 at 21:24
  • Duly noted, will discuss this with my team. – Arjun Nov 17 '11 at 21:25
  • 2
    @Kurtis: I tend to agree but I also see cases when the splash screen has sensible uses. E.g. when starting the app takes a long time. It's better to show a splash than a blank black screen for 15 seconds like Angry Birds does in my phone. – Jarno Argillander Nov 17 '11 at 21:49
  • 1
    @JarnoArgillander Fair enough. I totally agree that mobile games are a whole other ballpark. You can get away with a lot more in them than you can in other apps. – Kurtis Nusbaum Nov 17 '11 at 21:56
  • Okay, so I have a user constantly switching between activities, meaning they may have switched 15 activities in 20 seconds. Could that be a cause of an out of memory error? What should I do to fix it? Thanks! – Ruchir Baronia Jan 05 '16 at 05:40
  • @RuchirBaronia I have no idea. You should probably ask a new question with a full set of details about the problem. – Kurtis Nusbaum Jan 09 '16 at 06:35
2

A typical value of max application VM heap size is 24 MB. So, for instance, if your image is 10Mpx (3600 x 2400), then it would allocate 3600 x 2400 x 4 = 34'560'000 bytes which is an OutOfMemoryError case.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
  • My max image is only around 600kb (png) (1.7m px). – Arjun Nov 17 '11 at 21:30
  • @Arjun: This is already ~6.8 MB bitmap in RAM. Some older devices have just 16 MB VM heap limit. – Vit Khudenko Nov 17 '11 at 21:38
  • Ok, let me see about reducing it's size. Where did you get the "4" multiplier from? – Arjun Nov 17 '11 at 21:53
  • 1
    Do you have objects or references to objects in your code that you need to clean up? If you aren't going to be using them anymore, or for a while, you should set them to null, to free up memory. – Bryan Nov 17 '11 at 22:04
  • 1
    @Arjun: "4" multiplier is ARGB data per each pixel. Also make sure you don't keep anything leading to that bitmap statically. – Vit Khudenko Nov 17 '11 at 22:18
  • Okay, so I have a user constantly switching between activities, meaning they may have switched 15 activities in 20 seconds. Could that be a cause of an out of memory error? What should I do to fix it? Thanks! – Ruchir Baronia Jan 05 '16 at 05:40
  • @RuchirBaronia yes, there are chances to get out of memory if activities are leaked. you need to investigate you code for possible leaks. also you can try to use a library Leak Canary for that. https://corner.squareup.com/2015/05/leak-canary.html – Vit Khudenko Jan 05 '16 at 12:49
1

When dealing with Bitmaps in android, make sure you recycle the bitmap whenever you are done using it. You can load a resized bitmap by setting the inSampleSize option. More details here: http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize

Jing Wang
  • 50
  • 3