为什么在有足够内存的情况下会遇到“内存不足”?

时间:2012-08-06 09:13:30

标签: android

最近,我正在开发一个图像浏览应用程序。一个活动是在缩略图视图中浏览图像(活动A)(一个列表视图并嵌套一些gridview),在显示图像后,用户可以点击一张照片进入完整视图(活动B),然后进入编辑视图(活动C)。

在活动A中有很多照片,因此内存使用量增加(我没有限制缓存大小,40313k / 42503k),然后我进入活动B并释放活动A上的所有缓存,我可以看到内存丢失(26335K / 42503K)。但是,当我输入活动C时,会发生内存不足。我检查内存大小,可用内存仍然足够分配(52436K / 65159K分配7680016字节)

顺便说一句,我看到了Grow heap(frag case),这是什么?可能是原因吗?

有人可以帮忙吗?

以下是我的记忆日志

08-06 16:59:15.861: D/dalvikvm(29566): GC_FOR_ALLOC freed 1350K, 9% free 19414K/21319K, paused 32ms
08-06 16:59:16.064: D/dalvikvm(29566): GC_FOR_ALLOC freed 1350K, 9% free 19414K/21319K, paused 31ms
08-06 16:59:16.275: D/dalvikvm(29566): GC_FOR_ALLOC freed 1350K, 9% free 19414K/21319K, paused 34ms
08-06 16:59:16.463: D/dalvikvm(29566): GC_FOR_ALLOC freed 1350K, 9% free 19415K/21319K, paused 24ms
08-06 16:59:16.861: D/dalvikvm(29566): GC_FOR_ALLOC freed 1073K, 8% free 19699K/21319K, paused 18ms
08-06 16:59:17.338: D/dalvikvm(2923): GC_EXPLICIT freed 75K, 5% free 15254K/16007K, paused 5ms+2ms
08-06 16:59:17.400: D/dalvikvm(29566): GC_FOR_ALLOC freed 954K, 5% free 20446K/21511K, paused 19ms
08-06 16:59:17.752: D/dalvikvm(29566): GC_FOR_ALLOC freed 482K, 5% free 21515K/22599K, paused 20ms
08-06 16:59:22.377: D/dalvikvm(29566): GC_FOR_ALLOC freed 578K, 5% free 22566K/23623K, paused 40ms
08-06 16:59:22.416: D/dalvikvm(29566): GC_FOR_ALLOC freed 82K, 5% free 23245K/24391K, paused 15ms
08-06 16:59:22.424: I/dalvikvm-heap(29566): Grow heap (frag case) to 24.459MB for 1756816-byte allocation
08-06 16:59:22.439: D/dalvikvm(29566): GC_FOR_ALLOC freed 0K, 5% free 24961K/26119K, paused 15ms
08-06 16:59:22.603: D/dalvikvm(29566): GC_FOR_ALLOC freed 91K, 5% free 26006K/27143K, paused 16ms
08-06 16:59:22.744: D/dalvikvm(29566): GC_FOR_ALLOC freed 383K, 5% free 26994K/28167K, paused 15ms
08-06 16:59:22.838: D/dalvikvm(29566): GC_FOR_ALLOC freed 333K, 5% free 27931K/29191K, paused 15ms
08-06 16:59:22.932: D/dalvikvm(29566): GC_FOR_ALLOC freed 352K, 4% free 28902K/30087K, paused 16ms
08-06 16:59:23.033: D/dalvikvm(29566): GC_FOR_ALLOC freed 369K, 4% free 29863K/30983K, paused 16ms
08-06 16:59:23.127: D/dalvikvm(29566): GC_FOR_ALLOC freed 358K, 4% free 30808K/32007K, paused 16ms
08-06 16:59:23.221: D/dalvikvm(29566): GC_FOR_ALLOC freed 316K, 4% free 31764K/32903K, paused 16ms
08-06 16:59:23.322: D/dalvikvm(29566): GC_FOR_ALLOC freed 359K, 4% free 32847K/33927K, paused 16ms
08-06 16:59:23.416: D/dalvikvm(29566): GC_FOR_ALLOC freed 367K, 4% free 33798K/34951K, paused 17ms
08-06 16:59:23.510: D/dalvikvm(29566): GC_FOR_ALLOC freed 380K, 4% free 34875K/35975K, paused 17ms
08-06 16:59:23.619: D/dalvikvm(29566): GC_FOR_ALLOC freed 394K, 3% free 35951K/36999K, paused 17ms
08-06 16:59:29.392: D/dalvikvm(29566): GC_FOR_ALLOC freed 530K, 4% free 36919K/38087K, paused 19ms
08-06 16:59:29.486: D/dalvikvm(29566): GC_FOR_ALLOC freed 1127K, 6% free 36575K/38599K, paused 17ms
08-06 16:59:29.494: I/dalvikvm-heap(29566): Grow heap (frag case) to 37.010MB for 1267216-byte allocation
08-06 16:59:29.510: D/dalvikvm(29566): GC_FOR_ALLOC freed 127K, 6% free 37686K/39879K, paused 17ms
08-06 16:59:29.635: D/dalvikvm(29566): GC_FOR_ALLOC freed 638K, 4% free 38582K/39879K, paused 18ms
08-06 16:59:29.713: D/dalvikvm(29566): GC_FOR_ALLOC freed 1110K, 6% free 38246K/40263K, paused 17ms
08-06 16:59:29.721: I/dalvikvm-heap(29566): Grow heap (frag case) to 39.577MB for 2246416-byte allocation
08-06 16:59:29.752: D/dalvikvm(29566): GC_CONCURRENT freed 127K, 6% free 40313K/42503K, paused 2ms+3ms
08-06 16:59:39.963: D/dalvikvm(29566): GC_EXPLICIT freed 14180K, 39% free 26335K/42503K, paused 3ms+6ms
08-06 16:59:40.010: D/dalvikvm(29566): GC_FOR_ALLOC freed 3581K, 47% free 22844K/42503K, paused 21ms
08-06 16:59:40.017: I/dalvikvm-heap(29566): Grow heap (frag case) to 29.722MB for 7680016-byte allocation
08-06 16:59:40.064: D/dalvikvm(29566): GC_CONCURRENT freed 1K, 29% free 30342K/42503K, paused 2ms+8ms
08-06 16:59:51.111: D/dalvikvm(29566): GC_FOR_ALLOC freed 1803K, 33% free 28613K/42503K, paused 32ms
08-06 16:59:51.127: I/dalvikvm-heap(29566): Grow heap (frag case) to 35.355MB for 7680016-byte allocation
08-06 16:59:51.182: D/dalvikvm(29566): GC_CONCURRENT freed 4K, 28% free 36108K/50055K, paused 2ms+3ms
08-06 16:59:51.392: D/dalvikvm(29566): GC_FOR_ALLOC freed 32K, 28% free 36077K/50055K, paused 16ms
08-06 16:59:51.408: I/dalvikvm-heap(29566): Grow heap (frag case) to 42.644MB for 7680016-byte allocation
08-06 16:59:51.455: D/dalvikvm(29566): GC_CONCURRENT freed <1K, 25% free 43577K/57607K, paused 2ms+8ms
08-06 16:59:51.549: D/dalvikvm(29566): GC_FOR_ALLOC freed 7507K, 38% free 36091K/57607K, paused 23ms
08-06 16:59:51.557: I/dalvikvm-heap(29566): Grow heap (frag case) to 38.629MB for 3456016-byte allocation
08-06 16:59:51.619: D/dalvikvm(29566): GC_CONCURRENT freed 1K, 32% free 39464K/57607K, paused 2ms+3ms
08-06 16:59:51.682: D/dalvikvm(29566): GC_FOR_ALLOC freed 0K, 32% free 39465K/57607K, paused 17ms
08-06 16:59:51.697: I/dalvikvm-heap(29566): Grow heap (frag case) to 44.488MB for 6144016-byte allocation
08-06 16:59:51.728: D/dalvikvm(29566): GC_CONCURRENT freed 0K, 22% free 45465K/57607K, paused 1ms+3ms
08-06 16:59:51.900: D/dalvikvm(29566): GC_CONCURRENT freed 3717K, 25% free 43685K/57607K, paused 2ms+5ms
08-06 16:59:51.947: D/dalvikvm(29566): GC_FOR_ALLOC freed 710K, 24% free 43965K/57607K, paused 21ms
08-06 16:59:51.971: I/dalvikvm-heap(29566): Grow heap (frag case) to 50.348MB for 7680016-byte allocation
08-06 16:59:51.978: D/dalvikvm(968): GC_CONCURRENT freed 409K, 4% free 14423K/14983K, paused 1ms+1ms
08-06 16:59:52.025: D/dalvikvm(29566): GC_CONCURRENT freed <1K, 22% free 51465K/65159K, paused 1ms+9ms
08-06 16:59:52.127: D/dalvikvm(194): GC_EXPLICIT freed 983K, 19% free 20609K/25415K, paused 2ms+5ms
08-06 17:00:00.541: D/dalvikvm(29566): GC_FOR_ALLOC freed 531K, 20% free 52444K/65159K, paused 43ms
08-06 17:00:00.541: I/dalvikvm-heap(29566): Forcing collection of SoftReferences for 7680016-byte allocation
08-06 17:00:00.572: D/dalvikvm(29566): GC_BEFORE_OOM freed 7K, 20% free 52436K/65159K, paused 27ms
08-06 17:00:00.572: E/dalvikvm-heap(29566): Out of memory on a 7680016-byte allocation.    08-06 17:00:00.572: I/dalvikvm(29566): "Thread-1522" prio=5 tid=16 RUNNABLE
08-06 17:00:00.572: I/dalvikvm(29566):   | group="main" sCount=0 dsCount=0 obj=0x41c68058 self=0x3dc118
08-06 17:00:00.572: I/dalvikvm(29566):   | sysTid=29653 nice=0 sched=0/0 cgrp=default handle=4048232
08-06 17:00:00.572: I/dalvikvm(29566):   | schedstat=( 0 0 0 ) utm=4 stm=0 core=0
08-06 17:00:00.572: I/dalvikvm(29566):   at android.graphics.Bitmap.nativeCopy(Native Method)
08-06 17:00:00.572: I/dalvikvm(29566):   at android.graphics.Bitmap.copy(Bitmap.java:403)
08-06 17:00:00.572: I/dalvikvm(29566):   at com.mtn.atc.photoedit.PhotoEditor.applyEffect2Bitmap(PhotoEditor.java:627)
08-06 17:00:00.572: I/dalvikvm(29566):   at com.mtn.atc.photoedit.PhotoEditor.access$21(PhotoEditor.java:624)
08-06 17:00:00.572: I/dalvikvm(29566):   at com.mtn.atc.photoedit.PhotoEditor$2.run(PhotoEditor.java:613)
08-06 17:00:00.572: W/dalvikvm(29566): threadid=16: thread exiting with uncaught exception (group=0x40a521f8)

2 个答案:

答案 0 :(得分:0)

没有代码我只能想象总的来说,你只是在使用太多内存。

1)您是否在活动A中使用适配器视图(如listview),因此只加载可见的缩略图。 2)如果要加载完整图像并缩小缩略图,则在活动A中加载完整尺寸图像时,使用比例因子是值得的。

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

具体为http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;

Bitmap thumb = BitmapFactory.decodeFile(path, options);

这将大大减少活动A的内存使用量。

答案 1 :(得分:0)

当看似有足够的空闲堆时,分配失败可能是由here中的内存碎片引起的。如果您的目标是Android 3.0或更高版本,那么将android:largeHeap="true"添加到AndroidManifest.xml文件以尝试增加堆大小限制可能有所帮助,但这不是一个非常可靠的解决方案,并且无论如何都不会消除碎片。

相反,我建议创建一个可由新图像重用的位图静态池。