Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:11:16+00:00 2026-06-02T07:11:16+00:00

I’m working on an Android project which needs to be highly optimized (it’s an

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:11:18+00:00Added an answer on June 2, 2026 at 7:11 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.