I’m working on an Android project which needs to be highly optimized (it’s an SDK which is embedded in other apps, so obviously we want it to be as efficient and small as possible). This is relatively new to me, as in the past I have done mostly web/server development.
So here’s the question: when does it make sense to cache values as opposed to computing/fetching over again. Clearly the longer it takes to compute/fetch and the more often it is used, the better candidate it is for caching. But the dynamics/cost/reward of caching is quite different on a server app than in a mobile app.
On a server with lots of memory which is serving many simultaneous requests, it makes sense to cache values used repeatedly. On a mobile device, memory is constrained. Even if a value is used repeatedly, is it more efficient to cache it or to re-compute/re-read? (I’m not talking about network data, which clearly can benefit from caching locally. I’m talking more about values computed or parsed from a local file)
Here’s an example I’m been wondering about:
if (s_pattern == null) {
s_pattern = Pattern.compile(REGEX);
}
On the one hand, it makes sense to me to compile a RegEx only once and cache it, but on the other hand I’m not sure if there’s a lot gained to justify using additional memory.
Does anyone have any experience, or insights? Or do you have empirical data?
if (s_pattern == null) {
s_pattern = Pattern.compile(REGEX);
}
thats a good candidate to keep as constant (
[private|public|] static final PATTERN) if it never changes and is always used when the defining class is used.You should not get problems if you keep several of those or other “small” Objects. If you would recreate them each time you could even use more memory since you can have several instances of that object in memory – garbage collection does not remove immediately. Also constant object recreation will cost you CPU time which is also limited on mobile devices.
I would prefer caching over recreation whenever possible. Exception are big objects like bitmaps that use up several hundreds of kilobytes.
Almost all memory problems you see on Android are not related to intentional caching but to unintentional leaks or misuse of large Images.
Btw: this is not based on empirical data – but you could get some if you check what caused those: [android]+OutOfMemoryException