File I/O Vs. In Memory (best practice)

DHitchenUK

New Member
Licensed User
Longtime User
I'm a very experienced Windows programmer, but new to Android and Mobility.

Scenario: Let's say I have a list of 1,000 string items stored on the sd card, throughout the life of the app I'll want to read each item, randomly. I may read an item more than once and may alter the string and write it back.

My Question: Is it better (best practice, memory requirement) to read the items individually as needed and write back when updated, or (as I would in Windows) just load the strings into memory and write the file back when I update something.

My concerns are with the high I/O vs Memory consumption. If I access the file many times, there will be high seek and slow access, plus modifying an item would mean large writes. If I load it into memory there is a possibility that on lower end systems there will be a memory size issue?

Any experience or thoughts appreciated. How much memory does an app get?

Thanks in advance.
 

agraham

Expert
Licensed User
Longtime User
I would say it depends on the size of the strings but I'd probably go for in-memory storage. Compared to a bitmap strings are generally tiny. It is large bitmaps that are far more likely to give you memory problems than any other data. As characters are UTF-16 a 40 character string occupies less than 100 bytes including overhead. Using a couple of megabytes of data should be of no concern in an Android app so two megabytes would allow you about, say, 1000 strings each of nearly 1000 characters. In contrast that same two megabytes would give you an ARGB bitmap of only 512 x512 pixels.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…