Shared preferences are stored as key-value pairs inside an xml file. That's a fact. I haven't been able to find any documentation on the batter but ... when trying to find and load the value of a certain key, how does it actually look for the key inside the xml? Does it load the entire xml file into memory and then searches for the key or does it use some other method which is more memory friendly to go through each key?
I've come across an app which stores a ton of key-value pairs in shared preferences instead of using a database (there's A LOT!) and it is my hunch that this is quite memory intensive, as it will load the entire file into memory just to retrieve the specific item.
Can anyone clarify how this works "under the hood" ?
Thanks!
Yes, at least in the standard implementation. Technically,
SharedPreferences
is an interface, but AFAIKSharedPreferencesImpl
is the implementation thatContext
uses for methods likegetSharedPreferences()
.