4

Is it possible that a local (i.e. managed with LocalBroadcastManager) BroadcastReceiver leaks when the app is killed by the system?

The specific use case for which I need it is that I would like to register/unregister the BroadcastReceiver in an Activity's onCreate/onDestroy (I need the receiver to be active when the activity is not visible), but I wouldn't like to risk causing a memory leak by doing this. As I understand, if a single activity is destroyed by the system, onDestroy runs and the receiver is unregistered. What if the app is killed? Does the receiver remain around, or, being local, does it get killed along with the app?

pstobiecki
  • 1,804
  • 1
  • 17
  • 30

2 Answers2

4

If you peek inside LocalBroadcastManager source code, you'll see it's really just a normal class ("local" as you said) with a Map connecting the various BroadcastReceiver with the corresponding List of IntentFilter.

Nothing is registered at the "system level" so NO: if the app is killed, LocalBroadcastManager class is killed with it and no leak will occur.

Takhion
  • 2,706
  • 24
  • 30
0

You should absolutely unregister it in onDestroy because onDestroy doesn't mean your app is finished- just your Activity. Other Activities in your app could still be running, so that receiver would cause a leak.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thank you for your answer. However, what I meant by the question was the other case. The unregistering in OnDestroy might not happen when the system is killing the app, because the method will not be called. Is the receiver going to leak "out" in such a case? – pstobiecki Jun 27 '13 at 18:39