Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton with context as a variable - memory leaks?

Tags:

java

android

I am building a class to handle an app shared preferences. I want to make it singleton accessed from all parts of the project. I can't use the regular getInstance method as I need to pass a context for the class to be functional. I thought of passing a context in the MainActivity.java and than have it constantly there. Will there be memory leaks?

This is my current solution:

public class LocalStorage {

    private Context context;

    private static LocalStorage instance = null;

    protected LocalStorage() {
        // Exists only to defeat instantiation.
    }

    public static LocalStorage getInstance() {
        if(instance == null) {
            instance = new LocalStorage();
        }
        return instance;
    }

    public static LocalStorage getInstance(Context _context) {
        LocalStorage localStorage = getInstance();
        localStorage.context = _context.getApplicationContext();
        return localStorage;
    }

    private SharedPreferences sharedPreferences() {
        return context.getSharedPreferences("mySettings", Context.MODE_PRIVATE);
    }
 ....
like image 906
Guy Avatar asked Apr 23 '16 23:04

Guy


People also ask

What are the causes of memory leaks in programming?

If this is done several times, the part of memory assigned to the application may be exceeded causing the system to terminate the execution and the user to experience an app crash. Common causes of memory leaks are static variables, Singleton pattern, background tasks and anonymous inner classes.

Are managed memory leaks the same as unmanaged memory leaks?

You can easily allocate unmanaged memory yourself with special .NET classes (like Marshal) or with PInvoke. Many share the opinion that managed memory leaks are not memory leaks at all since they are still referenced and theoretically can be de-allocated. It’s a matter of definition and my point of view is that they are indeed memory leaks.

What is a memory leak in WPF?

The memory leak occurs when the binding mode is OneWay or TwoWay. If the binding is OneTime or OneWayToSource, it’s not a problem. Another WPF memory leak issue occurs when binding to a collection. If that collection doesn’t implement INotifyCollectionChanged, then you will have a memory leak.

How can my memory leak when there’s a garbage collector?

In a garbage collected environment, the term memory leak is a bit counter intuitive. How can my memory leak when there’s a garbage collector (GC) that takes care to collect everything? There are 2 related core causes for this. The first core cause is when you have objects that are still referenced but are effectually unused.


1 Answers

You will not create a leak since you are only keeping a reference to the application context by calling getApplicationContext().

If you referenced _context directly and that reference happened to be an Activity or some other Context then yes you would have created a memory leak.

As a side note the implementation of the Singleton here is a bit odd. I would suggest the following to remove the unnecessary extra getInstance() by passing in the Context to the constructor:

public class LocalStorage {
    private static LocalStorage instance = null;

    private Context context;

    private LocalStorage(Context context) {
        this.context = context;
    }

    public synchronized static LocalStorage getInstance(Context context) {
        if(instance == null) {
            instance = new LocalStorage(context.getApplicationContext());
        }

        return instance;
    }

    private SharedPreferences sharedPreferences() {
        return context.getSharedPreferences("mySettings", Context.MODE_PRIVATE);
    }
    ....
like image 182
George Mulligan Avatar answered Oct 28 '22 22:10

George Mulligan