安卓缓存>内部存储与对象缓存

发布于 2024-12-02 16:28:04 字数 621 浏览 1 评论 0原文

我需要缓存来自网络的图像(仅 5 个或最多 100 个)并显示在列表视图中。如果用户选择列表视图的一行,则可以清除缓存。我看了一些例子。有些使用外部存储。有些使用内部和外部。一些对象..

那么内部存储的优点/缺点是什么( http://developer.android.com/guide/topics/data/data-storage.html#filesInternal 通过 getCacheDir())和对象缓存(类似于 WeakHashMap 或HashMap < 字符串、软引用 < 可绘制 > )

软引用的一个问题似乎是它们可能会被垃圾收集得太快( 软引用也会被垃圾收集早)。安卓内部存储怎么样?参考文献说“当设备存储空间不足时,这些文件将首先被删除。”。

使用对象缓存或临时内部存储有什么区别吗?除了对象缓存应该更快一点

i need to cache images (only 5 or up to 100) from the web and displayed in a listview. if the user selects a row of the listview the cache can be cleared. i had a look on some examples. some use external storage. some use internal and external. some objects..

so what are the advantages/disadvantages of internal storage ( http://developer.android.com/guide/topics/data/data-storage.html#filesInternal via getCacheDir()) and object cache (something like WeakHashMap or HashMap < String, SoftReference < Drawable > )?

a problem with softreferences seems to be that they may get gc'ed too fast ( SoftReference gets garbage collected too early) . what about the android internal storage? the reference sais "These files will be ones that get deleted first when the device runs low on storage.".

does it make any difference to use a object cache or the temporary internal storage? except for the object cache should be a bit faster

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

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

发布评论

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

评论(2

琉璃繁缕 2024-12-09 16:28:04

以下是两者之间的一些区别:

  • 对象缓存比内部存储更快,但容量较低。
  • 对象缓存本质上是暂时的,而内部存储具有更长的生命周期
  • 对象缓存占用堆中的实际空间。内部存储则不然。这是很重要的一点,因为即使使用 SoftReference,使对象缓存太大也可能导致 OutOfMemoryException

现在考虑到这些差异,它们并不完全相互排斥。我们实现的很多功能都是使用多层缓存,特别是与图像加载相关的缓存。以下是我们使用的步骤:

  • 如果图像尚未缓存,则从 URL 获取并将其缓存在一级缓存中,即 SoftReference/WeakHashMap,甚至使用 LinkedHashMap 限制大小的硬缓存中。
  • 然后我们在中实现removeEldestEntry() LinkedHashMap。当达到硬缓存容量时,我们将数据移动到二级缓存,即内部存储。使用此方法,您不必从 URL 重新获取图像 + 它仍然更快,并且可以释放您的内存
  • 我们使用 LRU 算法在后台及时对内部存储进行清理。您不应该依赖 Android 来帮您清理这些问题。

我们已经将多层缓存作为一个通用组件,并在我们的客户的许多项目中使用了它。这种技术非常类似于计算机体系结构中的 L1、L2 缓存。

Here are the few differences between the two:

  • Object cache is faster than internal storage, but has lower capacity.
  • Object cache is transient in nature while internal storage has longer life span
  • Object cache takes the actual space in the heap. Internal storage doesn't. This is an important point, as making your object cache too large could cause the OutOfMemoryException even with SoftReference

Now given those differences, they are not totally mutually exclusive. A lot of we implemented is using multi layer caching especially related to the image loading. Here are the steps that we use:

  • If the image hasn't been cached, fetch from the URL and cache it in first level cache which is the SoftReference/WeakHashMap or even hard cache with limited size using LinkedHashMap
  • We then implement removeEldestEntry() in LinkedHashMap. Upon hitting the hard cache capacity, we move things to secondary cache which is the internal storage. Using this method, you don't have to refetch the image from the URL + it's still be faster and it frees up your memory
  • We ran a cleanup on timely basis on the background for the internal storage using LRU algorithm. You shouldn't rely on Android to clean this up for you.

We have made the multi layers caching a common component and have used it many of our projects for our clients. This technique is pretty much following to what L1, L2 cache in Computer Architecture.

机场等船 2024-12-09 16:28:04

You should look for the question "How do I lazy download images in ListView" and related.

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