【问题标题】:OutOfMemoryError in android on allocation 4Mb when 10Mb is free当 10Mb 空闲时,Android 中的 OutOfMemoryError 分配 4Mb
【发布时间】:2013-11-11 01:57:03
【问题描述】:

我不明白为什么在 4Mb 分配时会发生 OutOfMemoryError,因为我有 10Mb 的可用内存。为什么? (Android 4.1.2)

日志文件:

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)

【问题讨论】:

标签: java android out-of-memory


【解决方案1】:

当没有单个连续的堆空间块满足您请求的大小时,您将获得OutOfMemoryError。当仍然有大量可用的堆空间但它都是一系列较小的不连续块时,可能会发生这种情况。

例如,假设我们有一个 3K 堆,我们进行了三个 1K 分配:A、B 和 C。现在,我们的堆已用完,因为我们使用了 3K 堆中的所有 3K。

现在,A 被垃圾回收了。我们的堆在 1K 块中有 1K 的可用空间。如果我们尝试分配一个 1.5K 的块,我们会得到一个OutOfMemoryError,因为整体堆空间不足。

现在,C 被垃圾回收了。但是,A 和 C 是不连续的 - B 在它们之间。 A 和 C 的内存不能合并为一个块。因此,虽然我们有 2K 的可用堆空间,但在 1.5K 分配请求上我们仍然会失败并返回 OutOfMemoryError,因为没有单个连续的内存块可以满足所需的大小。只有当 B 也被垃圾回收时,我们的块才会合并回 3K 堆,此时我们可以授予 1.5K 分配。

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

【讨论】:

    【解决方案2】:

    如果出现这个错误有很多原因,众所周知,这是我们在加载高清图像时遇到的非常常见的问题,或者由于始终使用位图, 解决方案是:

    1. 使用低清晰度图片。
    2. 调整和回收位图。

    这里有一些有用的链接: Memory Leak and Out of memory Error

    Memory Leak and Out of memory Error using List,LinkedList and HashMap

    Memory Leak and Out of memory Error using LRU Cache

    Memory Leak and Out of memory Error using Disk LRU Cache

    【讨论】:

      猜你喜欢
      • 2015-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 2014-06-03
      • 2013-10-12
      • 1970-01-01
      相关资源
      最近更新 更多