-2

I'm trying to optimize my app against memory leaks. I've read A LOT of blog posts and SO questions, and I've seen some people saying to deference statically declared variables so the GC can collect them, while others saying it's unnecessary.

public class MyActivity extends Activity {

private static Context context;
private static ArrayList<String> arrayList = new Arraylist<>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        arrayList.add("Random strings");
..//Other code
    }

    @Override
    protected void onDestroy() {
        context = null;
        arrayList.clear();
        super.onDestroy();
    }

The reason behind the code is that static variables are independent of activity lifecycles and therefore will implicitly hold the reference for the entire activity context even after onDestroy unless it's reference is null. But I've tested both variants in my app and there weren't any differences from one another. Is the above really necessary?

Kyle
  • 598
  • 1
  • 7
  • 25

1 Answers1

2

I'm trying to optimize my app against memory leaks.

Then perhaps you should not be introducing memory leaks in the first place. Get rid of private static Context context and strongly consider getting rid of private static ArrayList<String> arrayList.

In standard Java, non-constant static fields are considered to be a serious code smell. They are used a bit more commonly in Android, but only where there is a clear reason to do so, and then only as a cache, and then only with more smarts managing that cache.

But I've tested both variants in my app and there weren't any differences from one another

You will see a difference if you add LeakCanary to your project. Without setting that static field to null, you are leaking this activity instance until such time as your process is terminated, or until such time as another instance of this activity is created (as that will replace the old field value).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you Mark. I saw your talk @Samsung about memory management - great information there too. The reason I'm asking is because the top answer to this question used a static context: http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android But thanks for confirming my thoughts – Kyle Feb 06 '16 at 13:58
  • 1
    @Kyle: That is an `Application`, not an `Activity`. There is a singleton `Application` instance in your process, created up front when the process is forked, before much of anything else of your app code runs. That `Application` instance lives for the life of the process, and there's nothing you can really do about that. In effect, it is "pre-leaked", and so having a `static` field pointing to it cannot leak it further. :-) That's not the case with most other things in Android, particularly activities, as they come and go. Personally, I consider that answer to be a code smell. – CommonsWare Feb 06 '16 at 14:01