Read speed of SharedPreferences

13,205

Solution 1

Is there a way to put them in memory for reading?

They are in memory, after the first reference. The first time you retrieve a specific SharedPreferences (e.g., PreferenceManager.getDefaultSharedPreferences()), the data is loaded from disk, and kept around.

Solution 2

My advice is to test your performance first, and then start worrying about speed. In general, you'll be happier with an app that prioritizes maintainability as well as speed. When engineers start out to achieve performance before they get the app stable, the result is an app that runs a bit faster but has lots of bugs.

Solution 3

According to this link, getSharedPreferences is not that much heavy because it opens file only when you call getSharedPreferences first time:

// There are 1000 String values in preferences

SharedPreferences first = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 4 milliseconds

SharedPreferences second = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 0 milliseconds

SharedPreferences third = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 0 milliseconds

But using get methods will take some time for the first time you call it:

first.getString("key", null)
// call time = 147 milliseconds

first.getString("key", null)
// call time = 0 milliseconds

second.getString("key", null)
// call time = 0 milliseconds

third.getString("key", null)
// call time = 0 milliseconds 
Share:
13,205
GLee
Author by

GLee

Updated on August 03, 2022

Comments

  • GLee
    GLee almost 2 years

    How fast are SharedPreferences? Is there a way to put them in memory for reading? I have a small amount of data that a ListView has to query to display each cell, and I'm worried that a call to flash memory will be too slow. I'm not worried about write speed, as writes will happen infrequently. I'm considering just using a JSON object to persist the data instead of SharedPreferences. Any thoughts?

  • Mohammad Ersan
    Mohammad Ersan over 10 years
    you say, the whole saved data in SharedPreferences will be loaded once you get instance of PreferenceManager.getDefaultSharedPreferences() ???
  • CommonsWare
    CommonsWare over 10 years
    @MoshErsan: Yes. The SharedPreferences is held in a static HashMap of SharedPreferences in ContextImpl. It will remain there until the process is terminated. See sSharedPrefs in github.com/android/platform_frameworks_base/blob/master/core‌​/…
  • Mohammad Ersan
    Mohammad Ersan over 10 years
    what is the benefit of doing it like this?!!
  • CommonsWare
    CommonsWare over 10 years
    @MoshErsan: Among other things, it ensures that all components of your app, asking for the same SharedPreferences, get the actual same SharedPreferences instance, and so they cannot get out of sync with each other.
  • Mohammad Ersan
    Mohammad Ersan over 10 years
    I was reading SharedPreferencesImpl->startLoadFromDisk, in this case if we saved a big data in it, it may cause OOE.
  • CommonsWare
    CommonsWare over 10 years
    @MoshErsan: Correct. SharedPreferences should be small.
  • user924
    user924 over 5 years
    @MohammadErsan for some data it's better to use database (SQLite) and SharedPreferences for some settings. I don't think your apps have a lot of settings
  • Ashton
    Ashton almost 4 years
    bad answer, you still have to load from disk, even you have the reference. Gosh!
  • CommonsWare
    CommonsWare almost 4 years
    @Devon: "you still have to load from disk" -- not according to the source code and the source code. Preferences and their values are cached.
  • Slion
    Slion almost 3 years
    So it looks like a "lazy“ implementation the loading happens on first get.
  • avisper
    avisper over 2 years
    amazing article! thanks for sharing