Android: OutofMemoryError: bitmap size exceeds VM budget with no reason I can see

前端 未结 5 1890
生来不讨喜
生来不讨喜 2020-11-30 07:43

I am having an OutOfMemory exception with a gallery over 600x800 pixels JPEG\'s.


The environment

I\'ve been using Gallery with JPG ima

相关标签:
5条回答
  • 2020-11-30 07:44

    The question was asked in 2010, when Froyo was fresh. So many things happened since. Before 3.0, bitmaps were allocated in JNI. The memory didn't show in the Dalvik stats. It doesn't have to be monolithic anymore. Before 2.3, memory statistics for JNI were not available (bitmap decoding calls JNI) in logcat. 4.4 evacuated more space. 5.0 the big bang of Art. Back in 2010, Nexus One was high end, with less than 300MB. The budget for an app was around 16MB. Now days, there is about 8 times that memory.

    0 讨论(0)
  • 2020-11-30 07:53

    I Also faced similar issue couple of weeks back and i solved it by scaling down images upto optimal point. I have written complete approach in my blog here and uploaded complete sample project with OOM prone code vs OOM Proof code here.

    0 讨论(0)
  • 2020-11-30 07:56

    There has been lots of time since I asked that.

    The answer should be divided to 2 parts: Pre-Gingerbread: you just use small pictures, use subsampling, maybe one screen size photo, and hope for good. Try to make sure you don't allocate tiny items you can't free before getting a bitmap. Pre-Ginger, the memory for bmps had to be contonuous, and it was not counted in the VM memory. Always look at the logcat. See a lecture about memory from Google IO 2011. Post Ginger it's easier. Since Honeycomb, bitmaps are even counted in your java area. There is no jni area. Always use recycle for bitmaps you don't need. Don't wait for the GC.

    0 讨论(0)
  • 2020-11-30 08:09

    I think there's nothing special in your case. There's just not enough memory. You can't have several 600x800 bitmaps in memory, they consume too much memory. You should save them to SD and load to memory on demand. I think that's exactly what you do.

    One thing you should be aware of: DDMS displays java heap memory consumption. But there's also native memory that is not displayed in DDMS. And bitmaps as far as I understand are created in native memory. So DDMS is just a bad tool to track these memory issues. You just need to be sure that you free your memory, that images are collected by Garbage Collector after you don't need them any more.

    Garbage Collector works on it's own schedule. That's why you should call Bitmap.recycle() method on bitmaps that you don't need any more. This method frees exactly the native memory that you run out of. This way you don't depend on GC and you can free largest piece of memory as soon as possible.

    First of all you should ensure that you don't leak bitmaps.

    Here's a nice post on memory allocations, it can help you to dig deeper

    0 讨论(0)
  • 2020-11-30 08:10

    Not sure if it's an option for you, but have you tried supersampling images Strange out of memory issue while loading an image to a Bitmap object?

    0 讨论(0)
提交回复
热议问题