简体   繁体   中英

OutOfMemoryError in android on allocation 4Mb when 10Mb is free

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 (eg, use inBitmap in BitmapOptions ).

there are many reasons if this error,and the one as we all know that,this is very common issue we face while loading a high definition image,or due to consistent use of bitmaps, solutions for this is:

  1. use low defination images.
  2. resize and recycle bitmaps.

and here are some useful links on this: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM