OutOfMemoryError in android when distributing 4Mb when 10Mb is free

I could not understand why OutOfMemoryError occurs when allocating 4Mb, because I have 10 MB of free memory. What for? (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) 
0
source share
2 answers

You will get an OutOfMemoryError when there is no single continuous heap block that matches your requested size. This can happen when there is still a lot of free space on the heap, but all this is a series of smaller uncomfortable blocks.

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

Now A receives the collected garabez. Our heap has 1K free space in a 1K block. If we try to allocate a 1.5K block, we get an OutOfMemoryError because there is not enough space for the heap.

Now C gets garbage collection. A and C, however, are incompatible - B is between them. Memory A and C cannot be combined into one block. Therefore, although we have 2 Kbytes of heap space, we will still fail with OutOfMemoryError on request for a 1.5K request, because there is no single contiguous block of memory that matches the desired size. Only if B also receives garbage collection, our blocks will be merged back into a 3K heap, and at this point we can provide 1.5K allocation.

That's why prudently recycling your allocated memory is important for Android when working with large images or other large blocks (for example, use inBitmap in BitmapOptions ).

+2
source

There are many reasons if this error and the way we all know that this is a very common problem that we encounter when loading high-definition images or due to the consistent use of bitmaps, solutions for this:

  • Use images with a low degree of protection.
  • Resize and recycle bitmaps.

and here are some useful links to 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 LRU Cache

0
source

Source: https://habr.com/ru/post/1239865/


All Articles