我不能理解为什么OutOfMemoryError会在4Mb的分配上发生,因为我有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)
发布于 2013-11-10 23:34:56
当没有一个连续的堆空间块满足您的请求大小时,您将获得一个OutOfMemoryError
。当仍然有大量的堆空间可用,但它都是一系列较小的不连续块时,就会发生这种情况。
例如,假设我们有一个3K的堆,我们进行了三个1K的分配: A、B和C。现在,我们的堆耗尽了,因为我们用光了3K堆中的所有3K。
现在,A得到garabage-收集。我们的堆在1K的块中有1K的空闲空间。如果我们尝试分配一个1.5K的块,我们将得到一个OutOfMemoryError
,因为总体上没有足够的堆空间。
现在,C被垃圾回收了。然而,A和C是不连续的-- B在它们之间。A和C的内存不能合并成一个单独的块。因此,虽然我们有2K的堆空间可用,但在1.5K的分配请求上,我们仍然会失败并返回OutOfMemoryError
,因为没有一个连续的内存块满足所需的大小。只有当B也被垃圾回收时,我们的块才会被合并回一个3K的堆,在这一点上,我们可以分配1.5K。
这就是为什么在处理大图像或其他大块时,智能地回收自己分配的内存在安卓中很重要(例如,在BitmapOptions
中使用inBitmap
)。
发布于 2013-11-11 01:57:04
这个错误有很多原因,正如我们都知道的,这是我们在加载高清晰度图像时面临的非常常见的问题,或者由于位图的一致使用,解决此问题的方法是:
这里有一些有用的链接: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
https://stackoverflow.com/questions/19891160
复制相似问题