2

我的活动有列表视图,并且(除了所有其他东西)从网络加载图像并将它们显示在列表视图中。我可以使用 5 台安卓设备:2 台 HTC 欲望、LG P-350、另外一部手机和一台平板电脑。通常情况下,一切正常,但在 HTC 的愿望之一上启动时,应用程序往往会因 NullPointerException 而崩溃,这是由于内存不足错误(我猜是这样),这是输出:

05-03 14:41:23.818: E/dalvikvm(843): Out of memory: Heap Size=7367KB, Allocated=4991KB, Bitmap Size=16979KB

稍后,logcat 输出 nullpointerexception 的堆栈跟踪,其中我的一个静态变量突然变为空(该变量在应用程序的根活动中初始化,在整个应用程序中使用,并且肯定不会在代码中为空)。我想,由于内存不足,它被系统清空了。据我了解,系统会尝试分配 17mb 的位图——我确信加载的图像不会那么大。它们是 100*70 jpeg,其中任何一个的重量都远小于 1mb。我不明白的另一件事是为什么我只在一个设备上收到此错误 - 其他设备工作正常。

在我看来,这看起来很奇怪,我找不到任何线索,我需要建议。

4

1 回答 1

2

The reason is simple: the memory is not holding your JPG data per say, but rather its decompressed equivalent, which, needless to say, takes a lot more RAM space than the source files... Note that this 17 mb limit is for all your loaded bitmaps at once, not necessarily a single one.

I had to fight with similar problems in one of my programs (a custom Tile loader for a Mapquest Android API MapView object), and I ended up having to call the recycle() method of my bitmaps whenever possible, as well as manually oblige the system to garbage collect at strategic locations using System.gc()...

Sorry to not be the bearer of the best news...

You might solve your problems using the same strategy as I did: I essentially cache the loaded bitmaps in hard storage such as my external SD card, and reload them on the fly when needed, instead of attempting to hold everything in RAM.

于 2012-05-03T11:28:36.273 回答