的OutOfMemoryError在分配4Mb的机器人时,为10Mb是免费的 [英] OutOfMemoryError in android on allocation 4Mb when 10Mb is free

查看:120
本文介绍了的OutOfMemoryError在分配4Mb的机器人时,为10Mb是免费的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么上的OutOfMemoryError 4Mb的分配,因为我有空闲内存为10Mb发生。为什么? (Android版4.1.2)

日志文件:

  14 11-9:37:12.503:D / MyApp的(1570):调试。 =================================
11-10 14:37:12.503:D / MyApp的(1570):debug.heap本地:16.61MB分配3.32MB(免费0.35MB)
11-10 14:37:12.503:D / MyApp的(1570):debug.memory:分配:32.00MB的30.00MB(免费8.00MB)
11-10 14:37:12.524:D / dalvikvm(1570):GC_FOR_ALLOC释放10K,免费22176K / 31111K 29%,暂停16ms的,16ms的总
11-10 14:37:12.524:I / dalvikvm堆(1570):强制SoftReferences的集合4431036字节分配
11-10 14:37:12.533:D / dalvikvm(1570):GC_BEFORE_OOM释放< 1K,免费22176K / 31111K 29%,11毫秒暂停,11毫秒总
11-10 14:37:12.533:E / dalvikvm堆(1570):出于对4431036字节分配内存。
11-10 14:37:12.533:I / dalvikvm(1570):线程-67PRIO = 5 TID = 10 RUNNABLE
11-10 14:37:12.533:I / dalvikvm(1570)|组=主SCOUNT = 0 dsCount = 0 = OBJ自我0xb59cfd68 = 0xb8e22fd8
11-10 14:37:12.533:I / dalvikvm(1570)| sysTid = 1587 =不错附表0 = 0/0 CGRP =的fopen错误:2] =办理-1193135816
11-10 14:37:12.533:I / dalvikvm(1570)| schedstat =(0 0 0)UTM = 245 STM = 91核心= 0
11-10 14:37:12.533:I / dalvikvm(1570):在android.graphics.Bitmap.nativeCreate(本机方法)
11-10 14:37:12.533:I / dalvikvm(1570):在android.graphics.Bitmap.createBitmap(Bitmap.java:640)
...
11-10 14:37:12.533:W / dalvikvm(1570):主题ID = 10:螺纹未捕获的异常退出(组= 0xb4ef4288)
11-10 14:37:12.543:E / AndroidRuntime(1570):致命异常:螺纹67
11-10 14:37:12.543:E / AndroidRuntime(1570):java.lang.OutOfMemoryError
11-10 14:37:12.543:E / AndroidRuntime(1570):在android.graphics.Bitmap.nativeCreate(本机方法)
11-10 14:37:12.543:E / AndroidRuntime(1570):在android.graphics.Bitmap.createBitmap(Bitmap.java:640)
11-10 14:37:12.543:E / AndroidRuntime(1570):在android.graphics.Bitmap.createBitmap(Bitmap.java:620)


解决方案

您将得到一个当出现堆空间,满足您的要求的大小没有单一的连续块的OutOfMemoryError 。当仍然有大量可用的堆空间会发生这种情况,但它毕竟是一系列较小的不连续的块。

例如,pretend我们有一个3K堆,我们提出三点1K分配:A,B和C.现在,我们的堆耗尽,因为我们用我们3K堆的所有3K <。 / p>

现在,A得到garabage回收。我们的堆有一个1K块空闲空间1K。如果我们尝试分配一个1.5K块,我们将得到一个的OutOfMemoryError ,因为整体的堆空间不足。

现在,C得到垃圾收集。 A和C,虽然是不连续的 - B在它们之间。 A的和C的内存不能被合并为一个单独的块。因此,虽然我们有2K的堆空间可用,我们仍然会失败与的OutOfMemoryError 在1.5K分配请求,因为内存能够满足所需的任何单一连续的块尺寸。只有当B也可以得到收集将我们的块被合并回一个3K堆的垃圾,在这一点上,我们可以授予1.5K分配。

这就是为什么智能回收处理大图像或其他大的块(例如,使用 inBitmap 打交道时,你自己分配的内存是Android的重要BitmapOptions )。

I couldn't understand why OutOfMemoryError occurs on 4Mb allocation cause i have 10Mb of free memory. Why? (Android 4.1.2)

log file:

11-10 14:37:12.503: D/MyApp(1570): debug. =================================
11-10 14:37:12.503: D/MyApp(1570): debug.heap native: allocated 3.32MB of 16.61MB (0.35MB free)
11-10 14:37:12.503: D/MyApp(1570): debug.memory: allocated: 30.00MB of 32.00MB (8.00MB free)
11-10 14:37:12.524: D/dalvikvm(1570): GC_FOR_ALLOC freed 10K, 29% free 22176K/31111K, paused 16ms, total 16ms
11-10 14:37:12.524: I/dalvikvm-heap(1570): Forcing collection of SoftReferences for 4431036-byte allocation
11-10 14:37:12.533: D/dalvikvm(1570): GC_BEFORE_OOM freed <1K, 29% free 22176K/31111K, paused 11ms, total 11ms
11-10 14:37:12.533: E/dalvikvm-heap(1570): Out of memory on a 4431036-byte allocation.
11-10 14:37:12.533: I/dalvikvm(1570): "Thread-67" prio=5 tid=10 RUNNABLE
11-10 14:37:12.533: I/dalvikvm(1570):   | group="main" sCount=0 dsCount=0 obj=0xb59cfd68 self=0xb8e22fd8
11-10 14:37:12.533: I/dalvikvm(1570):   | sysTid=1587 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1193135816
11-10 14:37:12.533: I/dalvikvm(1570):   | schedstat=( 0 0 0 ) utm=245 stm=91 core=0
11-10 14:37:12.533: I/dalvikvm(1570):   at android.graphics.Bitmap.nativeCreate(Native Method)
11-10 14:37:12.533: I/dalvikvm(1570):   at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
...
11-10 14:37:12.533: W/dalvikvm(1570): threadid=10: thread exiting with uncaught exception (group=0xb4ef4288)
11-10 14:37:12.543: E/AndroidRuntime(1570): FATAL EXCEPTION: Thread-67
11-10 14:37:12.543: E/AndroidRuntime(1570): java.lang.OutOfMemoryError
11-10 14:37:12.543: E/AndroidRuntime(1570):     at android.graphics.Bitmap.nativeCreate(Native Method)
11-10 14:37:12.543: E/AndroidRuntime(1570):     at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
11-10 14:37:12.543: E/AndroidRuntime(1570):     at android.graphics.Bitmap.createBitmap(Bitmap.java:620)

解决方案

You will get an OutOfMemoryError when there is no single contiguous block of heap space that meets your requested size. This can occur when there is still plenty of heap space available, but it is all a series of smaller discontiguous blocks.

For example, pretend that we had a 3K heap, and we make three 1K allocations: A, B, and C. Right now, our heap is exhausted, as we used all 3K of our 3K heap.

Now, A gets garabage-collected. Our heap has 1K of free space in a 1K block. If we attempt to allocate a 1.5K block, we will get an OutOfMemoryError, because there is insufficient heap space overall.

Now, C gets garbage-collected. A and C, though, are discontiguous -- B is in between them. A's and C's memory cannot be coalesced into a single block. Therefore while we have 2K of heap space available, we will still fail with an OutOfMemoryError on a 1.5K allocation request, because there is no single contiguous block of memory that meets the desired size. Only if B also gets garbage collected will our blocks be coalesced back into a 3K heap, at which point we can grant a 1.5K allocation.

This is why intelligently recycling your own allocated memory is important in Android when dealing with large images or other large blocks (e.g., use inBitmap in BitmapOptions).

这篇关于的OutOfMemoryError在分配4Mb的机器人时,为10Mb是免费的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆