0

I'm looking at the docs and trying to understand if the LruCache is competing for heap resources with the rest of my app or if it's actually using some sort of elaborate disk swapping mechanism.

The reason I'm asking is that I want to allocate a lot of memory for the cache (say a gig) so that if the device loses its Internet connectivity a large amount of image data can persis locally. However, if the LruCache is constrained by the size of the App's heap memory allotment this obviously won't work.

I'm hoping someone can point me to more detailed documentation about how this feature works and if this *isn't the right feature to handle this need, if I need to roll my own or if there's an Android native disk swap memory class that I'm overlooking that maybe should be combined with LruCache (or used entirely on its own?)

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236

1 Answers1

2

I'm looking at the docs and trying to understand if the LruCache is competing for heap resources with the rest of my app or if it's actually using some sort of elaborate disk swapping mechanism.

It is using heap space of your app. You can see the LRUCache source code to confirm this.

However, if the LruCache is constrained by the size of the App's heap memory allotment this obviously won't work.

Correct.

if I need to roll my own or if there's an Android native disk swap memory class that I'm overlooking

There is really no concept of "disk swap memory" at the Android app level, and very few devices even implement it at the OS level.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So... what (if anything) is my recourse if I want to hang on to some sizeable chunk of local imagery without running the risk of consuming more than N number of blocks on local storage? Am I totally out of luck here? – Yevgeny Simkin Nov 16 '16 at 19:51
  • @GeniaS.: "I want to hang on to some sizeable chunk of local imagery without running the risk of consuming more than N number of blocks on local storage?" -- I do not know what you consider "local storage" to mean. If you mean disk space, it is not like "disk swap memory" somehow magically uses no disk space. So, if your thinking was "I'll store the data in memory, to save disk space", that's kinda backwards, and in that case you are definitely out of luck. – CommonsWare Nov 16 '16 at 19:54
  • @GeniaS.: Typically, in Android, you start with a memory cache. If you need more room than that, or you want a cache to survive process termination (e.g., Web browser cache), you use a two-tier cache, backed by disk. You still cap the amount of disk space that you use, so users are unlikely to get angry with you. And if you have more stuff than that, use LRU and similar algorithms to maximize the cache value and minimize what you need to fetch again from the original data source (e.g., network). – CommonsWare Nov 16 '16 at 19:56
  • Yes, I mean disk space... I can envision some (reasonably simple) way to keep a folder of images which acts just like the LruCache (exactly as you describe above), but am unclear on if there's a pre-baked mechanism that I can just use or if I have to write this myself. It is precisely my desire to not annoy the user and limit this disk folder to N blocks (say a gig) – Yevgeny Simkin Nov 16 '16 at 20:00
  • @GeniaS.: There is no disk cache mechanism built into the Android SDK, though that would be nice. There may be some existing libraries for it, or you could look into apps that offer it (e.g., image loaders, OkHttp) and see what they do. – CommonsWare Nov 16 '16 at 20:04
  • @GeniaS.: FWIW, [here is a roster of caching libraries](https://android-arsenal.com/tag/14). – CommonsWare Nov 16 '16 at 20:32