0

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.

2 Answers2

1

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.

m0skit0
  • 23,052
  • 11
  • 71
  • 113
  • Thank you! I use the app myself so I'm considering buying a faster MicroSD card, since I find that the searching part is hardly time-consuming while reading and writing a real bottleneck – Sora Toshibe Jul 17 '12 at 08:01
  • 1
    You can optimize your search by for example only storing really needed data. For example, for files, you can only store the path and the line. When asked for more details, then you open the file and such. Try to avoid operations unless the user explicitly asks for them. This will surely make things faster and less memory consuming. – m0skit0 Jul 17 '12 at 09:34
1

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.

Community
  • 1
  • 1
Jeshurun
  • 21,639
  • 5
  • 75
  • 88