根据设备功能和可用内存调整 LRU 缓存大小

发布于 2025-01-08 17:15:03 字数 725 浏览 1 评论 0原文

我正在考虑在 Android 应用程序中实现第一层缓存。 我正在考虑使用 SoftReferences 来确保避免 OOM 异常,但由于有很多关于 Android 如何“过早”释放这些异常的文章,我决定研究一下 android.util.LruCache 缓存。

问题:如何根据实际设备正确调整大小? LRU 缓存才是真正的解决方案,而不是 SoftReferences,这听起来很不错,但如果您真的热衷于避免 OOM 异常,那么使用任意数量的兆字节硬引用会感觉非常不安全。如果你问我的话,那只是不安全。 无论如何,这似乎是唯一的选择。 我正在研究 getMemoryClass 以找出实际设备上应用程序的堆大小(+在调整缓存大小之前检查可用堆大小)。基线是 16 Megs,听起来不错,但我见过设备(例如过去的 G1)抛出 OOM 异常,堆大小约为 5 MB(根据 Eclipse MAT)。我知道 G1 已经很旧了,但重点是我的经验与文档提到的 16 Megs 基线并不相符。因此,如果我需要合理获得的最大缓存,我完全不确定应该如何扩展 LRU 缓存。 (对 8 Meg 很满意,并且在低规格设备上可以使用小至 1 Meg)

感谢您的任何提示。

编辑:我指的是Android LRU缓存类: http://developer.android.com/reference /android/util/LruCache.html

I'm thinking about implementing the first layer of my caching in an Android app.
I was considering SoftReferences to surely avoid OOM exceptions, but since there are many articles about how Android frees these up "too soon", I decided to look into android.util.LruCache cache.

Question: How do I size it up properly for the actual device?
It all sounds very nice that an LRU cache is the real solution and not SoftReferences, but if you're really keen to avoid OOM Exceptions, it feel extremely unsafe to go with any number of megabytes of hard references. It's just unsafe if you ask me.
Anyway, this seems to be the only option.
I was looking into getMemoryClass to find out the heap size of the app on the actual device (+checking the free heap size before sizing the cache up). The base line is 16 Megs which sounds Ok, but I've seen devices (G1 for example in the old days) throwing OOM exceptions just around 5 Megabytes of heap size (according to Eclipse MAT). I know a G1 is very old, but the point is that my experiences don't really align with the 16 Megs baseline the documentation mentions. Therefore I'm completely uncertain how should I scale up an LRU cache if I need the most I can reasonably get. (would be happy with 8 Megs and would go with as small as 1 Meg on a low-spec device)

Thanks for any hints.

Edit: The Android LRU cache class I'm referring to: http://developer.android.com/reference/android/util/LruCache.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终遇你 2025-01-15 17:15:03

我认为开发指南中概述了计算 LruCache 大小的有效解决方案:

int memClass = ( ( ActivityManager )context.getSystemService( Context.ACTIVITY_SERVICE ) ).getMemoryClass();
int cacheSize = 1024 * 1024 * memClass / 8;

可以在此处找到更多信息:http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

I think a valid solution to calculate the LruCache size is outlined in the dev guide:

int memClass = ( ( ActivityManager )context.getSystemService( Context.ACTIVITY_SERVICE ) ).getMemoryClass();
int cacheSize = 1024 * 1024 * memClass / 8;

More information can be found here: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

┈┾☆殇 2025-01-15 17:15:03

从你的问题来看,理解你所问的内容有点令人困惑。让我试一试。

各种缓存产品AppFabric、memcached、ncache 和scaleout 对每个对象都有1M 的限制。我认为横向扩展确实提供了某种定制。

但所有这些都是服务器端产品。因此,对于 Android 设备(很可能只是单个主机本地缓存),我可能会选择最大 64kb。我的意思是,为什么有人需要设备上每个对象超过 64kb。只是我的猜测。

如果我是你,我会研究 memcached(最著名的开源缓存解决方案)。并且可能是横向扩展的,因为它也很容易让 hello world 与横向扩展一起工作。并按比例决定。

From your question its a bit confusing to understand what you are asking. Let me give it a shot.

Various caching products AppFabric, memcached, ncache and scaleout have a 1M limitation on per object. I think scaleout does offer some kind of customization.

But all of these are server side products. So for a android device, which will most probably be a single host local cache only, I would probably go with a max of 64kb. I mean, why would anyone need more than 64kb per object on a device. Just my guess.

If I were you, I would study memcached (most famous open source caching solution). And may be scaleout since its easy to get a hello world working with scale out too. And proportionally decide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文