本文介绍了Android的内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我的Andr​​oid应用程序正在泄漏内存。我没有绝对的把握,这就是问题所在,虽然。

每隔一段时间开的时候,和logcat中显示了内存不足的异常试图加载一个位图图像应用程序崩溃。

崩溃后,我重新打开应用程序,并能正常工作。 LogCat中显示大量的GC秒和每一次的,而JIT表调整向上,永不向下,直到应用程序崩溃的内存不足的错误。

这听起来像一个内存泄露?如果是这样,我怎么去定位和关闭泄漏。

下面是我的亚行外壳meminfo中对我的应用程序。

  ** meminfo中的PID 2691 [com.example.deepcliff] **
                    本机Dalvik的其他总
            大小:23264 8839 N / A 32103
       分配:12503 3826 N / A 16329
            免费电话:168 5013 N / A 5181
           (PSS):2512 1395 13815 17722
  (共享脏):2088 1844年5008 8940
    (私法脏):2412 224 11316 13952

 对象
           浏览次数:0 ViewRoots:0
     AppContexts:0活动:0
          资产:2 AssetManagers:2
   当地粘合剂:55代理粘合剂:13
死亡收件人:1
 OpenSSL的套接字:0

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

 DATABASES
      pgsz dbsz后备(二)DBNAME
         1月14日10 webview.db
         1 6月18日webviewCache.db

 资产配置
    邮编:/data/app/com.example.deepcliff-2.apk:/resources.arsc:17K
 

解决方案

下面有一对夫妇的文章和帖子,这可能帮助你获得在正确的轨道:

分配跟踪的,它配备了Android SDK中是非常有用的。阅读罗曼盖伊的文章。它帮助我追查pretty的讨厌的泄漏。它还可以帮助你写出更好的软件。例如。我学会了创造较少的对象,使用更多的StringBuilder,并缓存了很多:
What Android的工具和方法的工作最好找内存/资源泄露?

有时候你的应用就是这么搞砸了,你必须重新设计它的整体。以下是官方,良好的提示(我最喜欢的是避免创建不必要的对象的):
http://developer.android.com/guide/practices/design/performance.html


下面是关于攻击你的内存问题一个很好的文章:
http://ttlnews.blogspot.com/2010/01/attacking-memory-problems-on-android.html

如何避免内存泄漏官方文章:
http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html

也阅读:工具来检查的android 内存泄漏


其他人已经指出有关的的位图的。这里有一篇文章描述了这个问题:http://zrgiu.com/blog/2011/01/android-bitmaps-and-out-of-memory-errors/

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
解决方案

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/

这篇关于Android的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:10