Android的位图,回收和堆碎片 [英] android bitmaps, recycling, and heap fragmentation

查看:180
本文介绍了Android的位图,回收和堆碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,使大量使用位图从SD卡读入。我敢肯定,99%,我的应用程序,而无需重复这些从来没有被破坏。它们都被分配到一个LRU缓存(commons-collections提供),其回收他们,因为他们是从LRU缓存中清除,而我在循环中的onDestroy剩余的()。

i have an app that makes heavy use of bitmaps read in from the SD card. i'm 99% sure my app is never destroyed without recycling these. they are all allocated into an LRU cache (commons-collections) which recycles them as they are purged from the LRU cache, and i recycle the remaining in onDestroy().

不过,我还是有问题。如果我开始/停止/启动/ ......我的应用程序几次,我得到了内存不足的错误,总是这样。

nonetheless, i still have problems. if i start / stop / start / ... my app several times, i get out of memory error, always.

我对这一理论的是,它必须做瓦特/ Android的非压缩堆。几次运行后,堆足够支离破碎所以虽然有足够的空闲内存,有大到足以载入位图不连续的块。这就是理论呢。

my theory on this is that it has to do w/ android's non-compacting heap. after several runs, the heap is sufficiently fragmented so although there is plenty of free memory, there's no contiguous chunks big enough to load the bitmaps. that's the theory anyway.

我被杀害我的过程的onDestroy解决这()(android.os.Process.killProcess())。这解决了问题,但引入了其自身的其他问题,因为我有其他线程和需要完善的服务,等待所有那些好听点是不实际的。更何况,我当然明白,这只是一个非常糟糕的做法。

i've been "solving" this by killing my process in onDestroy() (android.os.Process.killProcess()). this solves the problem, but introduces other issues of its own as i have other threads and services that need to complete, and waiting for all of those nicely is not practical. not to mention that i of course understand that this is just a really bad practice.

我读过所有的职位,等这个话题,我没有看到一个很好的解决方案。我使用Thumbnails.getThumbnail(),所以我已经使用缩小图像的版本。其中二次抽样的图像质量非常差进一步结果。

i've read all the posts, etc on this topic and i don't see a good solution. i'm using Thumbnails.getThumbnail(), so i'm already using scaled down versions of the images. subsampling them further results in very poor image quality.

我要留着8迷你缩略图和36微缩略图缓存,所以我从来没有比这更多的内存在的时间。这些都应该适合在一些兆在很大部分。

i'm keeping a cache of 8 "mini" thumbnails and 36 "micro" thumbnails, so i never have any more than that in memory at a time. these should all fit in a few megs at very most.

任何想法/提示/指针AP preciated。我在我的智慧对这个一端。

any ideas / tips / pointers are appreciated. i'm at my wit's end on this one.

推荐答案

您说您回收它们的onDestroy(),但也许你应该在<回收其中code>的onPause()?我碰到的一些列的切换时,方向和问题是由的onPause做回收()

You say that you recycle them in onDestroy(), but perhaps you should recycle them in onPause()? I've run into some out of memory issues when switching orientation and the issue was solved by doing recycling in onPause().

只是一个想法。不知道这会解决您的问题。

Just a thought. Not sure if that'll solve your issue.

这篇关于Android的位图,回收和堆碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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