Sora Toshibe
Sora Toshibe

Reputation: 3

Android. How to keep cache in RAM?

I need to search through text files (around 40MB) in my app with regular expressions, as you can imagine, it normally takes 1 minute or so to get it done. AND I have to do it repeatedly.

I wonder if I can keep these files in RAM after the first search. Can I possibly do that? I mean, find a way to explicitly say keep something in RAM for some time.

Upvotes: 0

Views: 1523

Answers (2)

Jeshurun
Jeshurun

Reputation: 23186

Consider putting your search results in a WeakHashMap, with keys that only exist for the duration that you need the values to exist, like the scope of an Activity. Watch out for memory issues though. On some devices, your application's process may only have a heap size as low as 16M.

Upvotes: 2

m0skit0
m0skit0

Reputation: 25873

Keep the results in a custom object that will save the search result. This will keep it in RAM (as long as you keep a reference to it).

Also keep in mind that allocating 40 MiB in RAM in Android devices is not a very good idea since RAM is quite limited in a lot of low-end devices. This can make your application a very tasty target for Android when it looks to free memory.

Upvotes: 1

Related Questions