Genymotion模拟器上的堆未扩展 [英] Heap not expanding on Genymotion emulator

查看:60
本文介绍了Genymotion模拟器上的堆未扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调整大型位图的大小以便更快地将图像上传到服务器时,我偶尔会遇到OutOfMemoryErrors. 为了避免这种情况,我计算了所需的内存量,并在尝试缩放图像之前检查它是否超过了Runtime.getRuntime().maxMemory().

While resizing large bitmaps for faster image upload to a server I occasionally ran into OutOfMemoryErrors. To prevent this I calculate the required amount of memory and check if it exceeds Runtime.getRuntime().maxMemory() before trying to scale an image.

但是,即使图像很容易放在堆上,我仍然遇到OOM错误.

However, I still run into OOM errors even though the image should fit on the heap easily.

使用上述方法,仿真设备(Galaxy SII API 16)为我提供了67108864字节的最大内存.

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

在以下代码段中,堆大小为43975K,并且仅<该内存的15K正在使用中.对于我的〜31K分配,堆应该自动增长到约45K,这甚至还没有接近64 MiB的最大大小. 但是正如您所看到的,dalvik vm并没有扩大堆,而是耗尽了内存.

In the following snippet, the heap size is 43975K and only < 15K of that memory is in use. For my ~31K allocation the heap should grow automatically 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, the 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.

我想知道这是否也可以在真实设备上发生,或者是否可能是genymotion的错误.

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

是否保证堆可以扩展到maxMemory()? JavaDoc for Runtime.getRuntime().freeMemory()表示,它可以"扩展,无论其含义是什么.

Is the heap guaranteed to expand up to maxMemory()? The JavaDoc for Runtime.getRuntime().freeMemory() says it "may" expand, whatever that means.

我只需要一种可行的方法来计算我可以使用的内存量,这就是我的方法,如果我错了,请纠正我:

I just need a realiable way to calculate the amount of memory I can use, this is how I did it, please correct me if I'm wrong:

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

此调用导致OutOfMemoryError:

This call causes the OutOfMemoryError:

// outOptions has an appropriate inSampleSize
BitmapFactory.decodeStream(inputStream, null, outOptions);

推荐答案

31961100字节分配导致内存不足

Out of memory on a 31961100-byte allocation

您的位图是32M. VM无法分配32M线性空间来存储位图.堆是零散的,因此即使您的堆具有32M可用空间,也不总是可能分配这样的线性空间.您可以尝试释放尽可能多的内存,并在解码流之前调用GC.

Your bitmap is 32M. VM can't allocate 32M linear space to store bitmap. Heap is fragmented, so even if your heap has 32M free space it is not always possible to allocate such linear space. You can try free up as much memory as you can and call GC before decoding stream.

尝试以更多的有效方式对位图进行解码. 或局部处理图像. 如果您告诉我们为什么需要这张图片,我们可以告诉您如何处理.

Try to decode your bitmap in more effective way. Or process image in parts. If you tell us why you need this image, we can tell you how to handle it.

这篇关于Genymotion模拟器上的堆未扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆