The heap does not apply to the Genymotion emulator

When resizing large raster images to load the image faster on the server, I sometimes encountered OutOfMemoryErrors. To prevent this, I calculate the required amount of memory and check if it exceeds Runtime.getRuntime (). MaxMemory () before scaling the image.

However, I still encounter OOM errors, although the image should easily be inserted into the heap.

The emulated device (Galaxy SII API 16) gives me a maximum memory of 67108864 bytes using the above method.

In the following fragment, the heap size is 43975 K and only <15K of this memory is used. For my distribution of ~ 31K, the heap should automatically grow to about 45K, which is still not even close to the maximum size of 64 MiB. But, as you can see, instead of expanding the heap, dalvik vm runs out of memory.

10-13 20:35:57.223: D/dalvikvm(1201): GC_FOR_ALLOC freed 505K, 67% free 14692K/43975K, paused 31ms, total 31ms 10-13 20:35:57.223: I/dalvikvm-heap(1201): Forcing collection of SoftReferences for 31961100-byte allocation 10-13 20:35:57.251: D/dalvikvm(1201): GC_BEFORE_OOM freed 2K, 67% free 14689K/43975K, paused 29ms, total 29ms 10-13 20:35:57.251: E/dalvikvm-heap(1201): Out of memory on a 31961100-byte allocation. 

I wonder if this could happen on a real device or if it could be a genymotion bug.

Is heap a guarantee of expanding to maxMemory ()? JavaDoc for Runtime.getRuntime (). FreeMemory () says that it can β€œexpand”, whatever that means.

I just need a real way to calculate the amount of memory that I can use, here is how I did it, please correct me if I am wrong:

 long maxMemory = Runtime.getRuntime().maxMemory(); long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long availableMemory = maxMemory - usedMemory; 

This call raises an OutOfMemoryError:

 // outOptions has an appropriate inSampleSize BitmapFactory.decodeStream(inputStream, null, outOptions); 
+6
source share
3 answers

Not enough memory allocated over 31961100 bytes

Your bitmap is 32M. VM cannot allocate 32M linear space for storing a bitmap image. The heap is fragmented, so even if your heap has 32M free space, it is not always possible to allocate such linear space. You can free up as much memory as you can and call GC before decoding the stream.

Try to decode your bitmap in a more efficient way. Or process the image in parts . If you tell us why you need this image, we can tell you how to deal with it.

+5
source

You have a 42 MB heap, of which 14 MB is already in use, 67% (28 M) are free / available

  D/dalvikvm(1201): GC_BEFORE_OOM freed 2K, 67% free 14689K/43975K, paused ... E/dalvikvm-heap(1201): Out of memory on a 31961100-byte allocation. 

You are trying to allocate ~ 31M (not 31K), which is larger than 28M, which is available, which leads to OOM.

Interpretation Details dalvikvm memory allocation log message take a look at debugging memory

Android uses a lot of shared memory usage to correctly calculate the use of each process memory in order to pass this SO question

Android guidelines for efficient raster memory management can help

+1
source

One thing you can try to customize: build.props ROM file.

In the Genymotion emulator, you can try the following through the root shell:

 cat /system/build.prop | grep dalvik 

and it will display the dalvik settings line:

 dalvik.vm.heapsize=256m dalvik.vm.lockprof.threshold=500 dalvik.vm.stack-trace-file=/data/anr/traces.txt 

And maxmemory also reported as 268435456 bytes on the emulator that I experimented with.

So, you can try playing with this parameter. In addition, make sure that the memory allocated in the VirtualBox settings is compatible with these values.

+1
source

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


All Articles