Reputation: 187
I build my first android app. I have a collection of lists that I display. I'm wondering with a phones small memory size, is it a better design approach to go the database for my list every time or keep it in memory?
I do need to frequently re-order lists, so by just grabbing them out of the database in a sorted order it is pretty fast.
Upvotes: 0
Views: 143
Reputation: 29436
In-memory Lists use RAM, frequent DB queries use CPU and cause I/O lag. On a mobile device you have to balance all of these to make your code responsive, yet spare battery and have least impact on RAM and CPU.
Set reasonable limits for your code and try to work within. Decide on a maximum memory limit and a minimum response time.
Keep Data in memory as long you have RAM to spare, start reading/writing parts of that data to disk DB when you exceed RAM limits (paging). You will need this only in rare cases, because most modern mobile devices have reasonable RAM for the average demands of Apps they offer.
A typical example would be showing a list of a million items, where you keep 1000 or so in RAM and load next/previous 1000 from DB on demand.
Finally make sure to persist important data to DB when you App is not around anymore.
Upvotes: 1
Reputation: 155
Databases should only be used to store non setting information between power cycles or between applications. You shouldn't use them for something you can put in memory because it will be slower and a lot of extra programming.
If you need to process massive amounts of lists and have a good way to organize them them tables, retrieving them in SQL, I could potentially see using database to keep the data in persistent memory to be useful.
Edit:
Also you may want to consider how frequent the data is being accessed. You don't want to query a database constantly.
Upvotes: 0
Reputation: 44228
we don't know the contents of your lists, but generally you won't be encroaching on your device's or your app's memory limit unless you are dealing with graphics.
Textual data and structures that deduce to simply data types will not use up your device's "limited memory", unless of course they are really large.
Anyway my answer would be to keep it in memory. Only persist data that you need.
Upvotes: 0