开发者

Out Of memory error - Bitmap exceeds VM budget in Android while using bitmaps in maps?

I am plotting images of height and width equal to device screen as an overlay item in Android. But when the number of images exceeds the app crashes with Out Of Memory error - Bitmap exceeds VM budget. If I try to recycle it then the error comes as canvas trying to use recycled bitmap. What I need is to have the best way of plotting lots of images over map. The images comes from the server and I also need to cache the images. For caching currently I am 开发者_开发百科doing that in LinkedHashMap but I guess even this will create problems as I am storing the drawable objects.

Is there any example of using plotting large bitmaps on maps?


You might want to consider using the inSampleSize property of the BitmapFactory.Options class. This property will rescale your image by a power of 2 (which you decide) when the bitmap object is created, which will save memory. If the resolution of the picture is greater than the resolution of the screen, this should work perfectly well for you, without degrading the quality of the picture.

To use this for an image you are downloading from a server, you can use it as follows:

URL url = new URL(photoUrl);
URLConnection ucon = url.openConnection();

Options options = new Options();
options.inSampleSize = 2;

Bitmap bitmap = BitmapFactory.decodeStream(ucon.getInputStream(),null, options);

An alternative is to also wrap each bitmap object in your HashMap with a SoftReference object so that the VM will reclaim the memory used by bitmaps, rather than crashing with an OOM error. The downside is that you would have to reload the bitmap and personally, I feel that the VM is aggressive when reclaiming memory. It reclaims memory pretty quickly.


Each application has very limited amounts of memory (often 16MB, but I've seen as low as 14MB, and as high as 32MB). This is specific to the firmware running on each device. The application class can be inherited to provide access to a "LowMemory" function, which can alert you when your application has used almost all of its memory.

You shouldn't have problems with images the size of the screen, but make sure they're not ridiculously over-sized (have them as small as possible).

I'd also recommend checking out the memory stat in Eclipse to see how much memory your application is using (to see how it grows etc).

This error is one of the most annoying errors when dealing with Maps and Android and you will find plenty of other posts related to this issue here on SO

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜