Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android performance: cost of SharedPreferences

When my app starts, I populate a container class with values from my shard prefs. The idea was to handle the SharedPreferences and PreferenceManager once since I'm guessing they're heavy.
Here's a sample:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cont); 
StorageClass.lifespan = Integer.parseInt( prefs.getString("lifespan", "8") );
StorageClass.hiRate = Integer.parseInt( prefs.getString("hiRate", "71") );
//and on and on for all preferences 

Other activities then request these values.
But as my app grows, main memory is becoming tight.

Would it be better to have every requester make an instance of SharedPreferences and get the value they want?

Thanks

like image 905
soBinary Avatar asked Jul 13 '11 16:07

soBinary


1 Answers

The same SharedPreferences object is returned each time you request the preferences of the same name. If that wasn't the case, there would need to be a lot of complicated code inside the framework to deal with multiple SharedPreference objects keeping their state in sync when it is changed by one of them.

(This is kind-of the idea behind calling it "shared" preferences.)

like image 51
hackbod Avatar answered Nov 16 '22 19:11

hackbod