Android OOM,永远不会释放内存

时间:2012-02-02 10:15:43

标签: android memory memory-management memory-leaks out-of-memory

我正在开发Android 2.3.3 API Lv10。我的应用程序使用' LoaderManager'在后台加载图像的界面,图像显示在ViewPager中。如果我重复查看图像,应用程序最终会崩溃。看起来应用程序快速分配内存,而GC无法释放其中的一部分。

有没有办法跟踪内存中的哪个对象?

02-02 18:02:05.564: D/dalvikvm(255): GC_EXPLICIT freed 500K, 55% free 2599K/5703K, external 1625K/2137K, paused 534ms
02-02 18:02:05.714: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 365K, 54% free 2680K/5703K, external 3045K/3338K, paused 54ms
02-02 18:02:06.074: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 59K, 54% free 2677K/5703K, external 4211K/5259K, paused 62ms
02-02 18:02:10.645: D/dalvikvm(316): GC_EXPLICIT freed 322K, **54% free** 2539K/5511K, external 1625K/2137K, paused 100ms
02-02 18:02:53.054: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 109K, **53% free** 2684K/5703K, external 6691K/6808K, paused 48ms
02-02 18:03:16.834: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 604K, 55% free 2720K/5959K, external 8954K/9904K, paused 52ms
02-02 18:03:18.304: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 114K, 55% free 2711K/5959K, external 6160K/6211K, paused 47ms
02-02 18:03:21.534: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 128K, 55% free 2731K/5959K, external 8375K/9165K, paused 38ms
02-02 18:03:29.154: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 612K, 55% free 2757K/6023K, external 10236K/11261K, paused 50ms
02-02 18:03:30.394: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 90K, 55% free 2719K/6023K, external 8070K/8080K, paused 45ms
02-02 18:03:33.944: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 571K, 54% free 2786K/6023K, external 9093K/10112K, paused 74ms
02-02 18:03:36.304: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 115K, 54% free 2777K/6023K, external 7952K/8724K, paused 45ms
02-02 18:03:41.194: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 319K, 53% free 2877K/6023K, external 10954K/10980K, paused 51ms
02-02 18:03:58.054: I/dalvikvm(389): Jit: resizing JitTable from 512 to 1024
02-02 18:03:58.454: D/dalvikvm(389): GC_CONCURRENT freed 1254K, 58% free 2841K/6727K, external 8152K/8375K, paused 4ms+5ms
02-02 18:04:01.314: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 80K, 58% free 2847K/6727K, external 8078K/8375K, paused 57ms
02-02 18:04:02.244: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 66K, 58% free 2884K/6727K, external 10625K/11366K, paused 40ms
02-02 18:04:07.614: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 191K, 58% free 2874K/6727K, external 13698K/14474K, paused 49ms
02-02 18:04:16.104: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 566K, 57% free 2925K/6727K, external 12504K/12547K, paused 41ms
02-02 18:04:16.574: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 194K, 58% free 2837K/6727K, external 8589K/9589K, paused 51ms
02-02 18:04:18.774: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 71K, 58% free 2862K/6727K, external 9048K/11075K, paused 40ms
02-02 18:04:20.705: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 108K, 58% free 2859K/6727K, external 11804K/12545K, paused 49ms
02-02 18:04:22.475: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 52K, 58% free 2861K/6727K, external 14210K/16258K, paused 40ms
02-02 18:04:24.044: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 52K, 58% free 2863K/6727K, external 16920K/18968K, paused 47ms
02-02 18:04:24.153: E/dalvikvm-heap(389): 1232160-byte external allocation too large for this process.
02-02 18:04:24.255: D/dalvikvm(389): GC_FOR_MALLOC freed <1K, 58% free 2863K/6727K, external 16920K/18968K, paused 29ms
02-02 18:04:24.265: W/dalvikvm(389): threadid=9: thread exiting with uncaught exception (group=0x40015560)
02-02 18:04:24.565: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 91K, 58% free 2850K/6727K, external 16967K/18968K, paused 149ms
02-02 18:04:25.664: D/dalvikvm(389): GC_FOR_MALLOC freed 0K, 58% free 2850K/6727K, external 17021K/18968K, paused 161ms
02-02 18:04:27.144: D/dalvikvm(389): GC_EXTERNAL_ALLOC freed 39K, 58% free 2880K/6727K, external 17000K/18968K, paused 49ms
02-02 18:04:27.364: I/dalvikvm-heap(389): Clamp target GC heap from 24.004MB to 24.000MB
02-02 18:04:27.374: D/dalvikvm(389): GC_FOR_MALLOC freed <1K, 58% free 2880K/6727K, external 17048K/18968K, paused 29ms
02-02 17:48:09.424: D/dalvikvm(337): GC_CONCURRENT freed 1400K, 61% free 2686K/6727K, external 3045K/3330K, paused 4ms+4ms
02-02 17:48:10.284: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 58K, 60% free 2707K/6727K, external 2972K/3330K, paused 37ms
02-02 17:48:11.664: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 78K, 60% free 2709K/6727K, external 3842K/4799K, paused 44ms
02-02 17:48:17.364: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 161K, 60% free 2719K/6727K, external 5843K/6032K, paused 41ms
02-02 17:48:25.824: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 157K, 60% free 2726K/6727K, external 8108K/8682K, paused 76ms
02-02 17:48:30.084: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 157K, 60% free 2732K/6727K, external 10488K/11355K, paused 63ms
02-02 17:48:39.704: D/dalvikvm(337): GC_CONCURRENT freed 742K, 58% free 2888K/6727K, external 12840K/13865K, paused 4ms+3ms
02-02 17:48:43.895: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 893K, 60% free 2749K/6727K, external 3941K/4896K, paused 52ms
02-02 17:48:58.414: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 160K, 60% free 2750K/6727K, external 6400K/6445K, paused 55ms
02-02 17:49:02.324: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 375K, 59% free 2821K/6727K, external 9503K/9541K, paused 43ms
02-02 17:49:03.755: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 320K, 59% free 2777K/6727K, external 4944K/5945K, paused 69ms
02-02 17:49:08.184: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 212K, 59% free 2767K/6727K, external 7593K/7630K, paused 46ms
02-02 17:49:10.914: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 451K, 59% free 2805K/6727K, external 5893K/6353K, paused 50ms
02-02 17:49:15.294: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 163K, 58% free 2843K/6727K, external 9263K/9265K, paused 39ms
02-02 17:49:16.434: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 499K, 58% free 2835K/6727K, external 6842K/7538K, paused 76ms
02-02 17:49:18.824: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 109K, 58% free 2831K/6727K, external 9008K/9765K, paused 43ms
02-02 17:49:25.774: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 645K, 59% free 2816K/6727K, external 12225K/12235K, paused 53ms
02-02 17:49:35.494: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 60K, 58% free 2864K/6727K, external 10256K/12304K, paused 54ms
02-02 17:49:59.824: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 1593K, 60% free 2905K/7111K, external 13747K/14632K, paused 58ms
02-02 17:50:02.764: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 114K, 59% free 2927K/7111K, external 11181K/12092K, paused 73ms
02-02 17:50:04.605: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 151K, 59% free 2935K/7111K, external 13655K/14466K, paused 47ms
02-02 17:50:09.174: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 513K, 59% free 2970K/7111K, external 15783K/15803K, paused 49ms
02-02 17:50:14.434: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 247K, 59% free 2926K/7111K, external 11360K/11714K, paused 52ms
02-02 17:50:20.464: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 608K, 59% free 2960K/7111K, external 13909K/14934K, paused 50ms
02-02 17:50:24.415: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 143K, 59% free 2980K/7111K, external 14651K/15393K, paused 43ms
02-02 17:50:24.655: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 0K, 59% free 2980K/7111K, external 14651K/15393K, paused 49ms
02-02 17:50:24.725: I/dalvikvm-heap(337): Clamp target GC heap from 24.113MB to 24.000MB
02-02 17:50:24.725: D/dalvikvm(337): GC_FOR_MALLOC freed <1K, 59% free 2980K/7111K, external 17058K/19106K, paused 59ms
02-02 17:50:24.805: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed <1K, 59% free 2980K/7111K, external 17058K/19106K, paused 69ms
02-02 17:50:25.045: E/dalvikvm-heap(337): 2774400-byte external allocation too large for this process.
02-02 17:50:25.115: I/dalvikvm-heap(337): Clamp target GC heap from 24.112MB to 24.000MB
02-02 17:50:25.115: D/dalvikvm(337): GC_FOR_MALLOC freed 0K, 59% free 2980K/7111K, external 17058K/19106K, paused 56ms
02-02 17:50:25.304: E/GraphicsJNI(337): VM won't let us allocate 2774400 bytes

2 个答案:

答案 0 :(得分:2)

@JoxTraex给出的链接很棒。但对于这个问题,我终于找到了根本原因。它是 LoaderManager ,它包含对Loader对象的引用以及 Loader.loadInBackground 方法返回的对象。在调用LoaderManager.destroyLoader( id )之后,问题就永远消失了。

答案 1 :(得分:1)

这是一个很难解决的话题,特别是如果你保留对你的位图的引用,那么你开始重载内存。这是因为图像在内存/ VM方面非常繁重。你应该看一下Romain Guy关于图形和位图以及如何处理它的内容。

下面:

http://www.youtube.com/watch?v=duefsFTJXzc

http://www.youtube.com/watch?v=wJYTBjMZJh0

它们是冗长的视频,但有很多好的信息,其中一些包括如何使用最佳实践来处理这些信息。