Android memory leak?

2019-01-21 04:05发布

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

4条回答
做自己的国王
2楼-- · 2019-01-21 04:42

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/

查看更多
等我变得足够好
3楼-- · 2019-01-21 04:50

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.

查看更多
成全新的幸福
4楼-- · 2019-01-21 04:55

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

查看更多
太酷不给撩
5楼-- · 2019-01-21 05:06

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.

查看更多
登录 后发表回答