what is the difference between shared preferences and internal storage in Android? Where does the data is stored? Looks like Android system allocates specific amount of space for each application. While using shared preferences, my app ran out of memory and threw OutOfMemory Exception. If I use internal storage to save this data into files, will this resolve the issue? If internal storage also uses the same limited allocated space for the app, then how to solve this issue?
SharedPreferences stores data in key-value pairs. It stores them mainly in RAM but it also saves a copy to the internal storage. Android provides RAM for storing your code, all the graphics and any temporary data, and it's limited. If you store a lot of key-value pairs (and maybe the values are long String
s), you may indeed use all the RAM for your app and end up with an OutOfMemoryException
. That's an indication that SharedPreference is probably not the right method for the data you're trying to store.
The internal storage, instead, is entirely based on Flash memory. Apps have less constraints there, and they can store large amounts of data like images. The internal storage is kinda like a directory, so you create files, read/write on them, delete them etc., so it's different than key-value pairs.