1

I keep getting a warning for a memory leak for my static fragment instance. I declare it as follows:

private static myFragment thisFragment;

and call it here:

public static myFragment newInstance() {

        if (thisFragment == null) {
            thisFragment = new myFragment();
        }
        return thisFragment;
    }

How do I fix this? any ideas?

Thanks!

2
  • 1
    What's the purpose of holding a static reference to the fragment? Aug 3, 2017 at 21:23
  • 1
    Just create a new fragment instance every time the method is called, like the method name suggests.
    – BladeCoder
    Aug 3, 2017 at 21:32

2 Answers 2

9

Well, there are 3 possible solutions:

1) Not creating static Fragments. They are always bound to the context, so, they shouldn't be static. Consider refactoring your app architecture.

2) Setting fragment instance to null when context is destroyed (activity onStop)

3) Using WeakReference for fragment field which will not hold the instance from GC.

UPD: Example for 3)

class ExampleFragment extends Fragment {

    private static WeakReference<Fragment> instance;

    public static ExampleFragment getInstance() {
        if (instance == null) {
            instance = new WeakReference<>(new ExampleFragment());
        }
        return instance.get();
    }
}
13
  • can you give me examples of each with respect to my code, would be greatly helpful.
    – user2386226
    Aug 3, 2017 at 21:23
  • Hmm. I need more of your code to provide an examples for 1) or 2). But 3) is pretty trivial. Updated my answer Aug 3, 2017 at 21:24
  • with regards to 2, onstop, i set thisfragment = null; , will that be sufficient?
    – user2386226
    Aug 3, 2017 at 21:25
  • Yep, every of that solution is enough :). Practically, when you set thisfragment to null you free the context and let GC to collect it Aug 3, 2017 at 21:28
  • thats awesome, thanks so much, i like the weakreference usage. onstop is not guaranteed. Also, in my baseapplication file, I have another static variable with potential for leaks, some customconnection parameter, is it possible to set weakreference for that too and not just fragments?
    – user2386226
    Aug 3, 2017 at 21:33
1

You should never ever ever ever EVER hold a static reference to a Context.

WHY do you need to hold a static reference to a fragment? If you answer that, then we can guide you on how to avoid it.

Static references never die, and that fragment context is most likely linked to an Activity context at some point, which means that ALL those elements can never ever be garbage collected. This is really bad.

1
  • 1
    Hm... holding fragment in static reference is absolutely bad. But holding context in static field may be safe if you use WeakReference or free the link in time. Also it's ok to hold application context in static Aug 3, 2017 at 21:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.