Performace-Create object in every class or use static class for custom sharedpreferences

Ritu Source

I have custom sharedpreferences class where i have lots of getter and setter methods. I am setting values to sharedpreferences which is i am accessing in different classes.So my question is when i am accessing custom sharedpreferences in any class first i create its object like this.

UserSession session=new UserSession(getactivity,"customSharedpreferences");

I feel its costly as performance wise to create object everytime i need value from sharedpreferences and thinking to make it static class but i am not sure which bet is good as performance based.

Access without creating object of classUserSession.getEmailAddress(); or create object first then session.getEmailAddress();

public class UserSession {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    public static final String TAG="USER SESSION######";

    public UserSession(Context context,String preferenceFileName ){
            sharedPreferences = context.getApplicationContext().getSharedPreferences(preferenceFileName, Context.MODE_PRIVATE);



answered 2 weeks ago karandeep singh #1

I think using a singleton pattern is useful here if you need to access that object frequently. Because, if you create a new object every time, Garbage collector will be called frequently, which should be avoided. Quoting form android documentation,

Object creation is never free. A generational garbage collector with per-thread allocation pools for temporary objects can make allocation cheaper, but allocating memory is always more expensive than not allocating memory.

As you allocate more objects in your app, you will force a periodic garbage collection, creating little "hiccups" in the user experience. The concurrent garbage collector introduced in Android 2.3 helps, but unnecessary work should always be avoided.

answered 1 week ago sziraqui #2

It's better to directly get value from shared preferences instead of creating a class. But if you still want to create a class for easier usage across activities, its best to use SQLite and create DatabaseHelper class and use similar getter-setter methods. This is much more efficient than your custom shared preferences implementation

Here's a great tutorial on SQLite

answered 1 week ago Caleb #3

The answer depends a little on what role you want UserSession to fill. Do you want UserSession to be a simple wrapper for SharedPreferences or do you want it to be a single source of truth for your session data?

If you want it to act simply as a wrapper then I would make it a static class which does not store the context. Just pass the context and shared preferences in to each method that needs it.

public String getUserName(Context ctx, String sharedPrefFileName) { your tedious shared preference editing here...}

If you want the UserSession to store the state of the current "session", then I would recommend making it a singleton. This singleton could store the shared preference filename. It could also store a reference to context but you have to be careful how you construct this singleton.

You want to be careful not to leak your context reference. I don't know the lifecycle of UserSession but you should be careful when handing your context to it.

There are several different ways that you can 'store' context in a singleton. Android documentation says that you can make a custom Application class which exposes itself via a static public reference or... (from android docs)

In most situations, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), include Context.getApplicationContext() as a Context argument when invoking your singleton's getInstance() method.

I'm assuming UserSession calls getSharedPreferences to get a reference to the SharedPreferences. This method will always return the same instance (per filename) so you don't need to worry about calling this a lot since it doesn't create new instances.

is it ok to make singleton my custom sharedpreferences class which has around 100 getter and setter methods

You can always break out the methods into smaller more modularized classes but it won't change the performance of your application. The number of methods in the singleton shouldn't affect your performance.

comments powered by Disqus