Local broadcast receiver leak?

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

The specific use case for which I need it is that I would like to register / unregister BroadcastReceiver in Activity onCreate / onDestroy (I need the receiver to be active when the activity is not visible), but I wouldn’t, for example, at the risk of causing a memory leak by doing so. As far as I understand, if one action is destroyed by the system, onDestroy is launched, and the receiver is not registered. What if the application is killed? Does the receiver remain, or, being local, is it killed along with the application?

+4
source share
2 answers

If you look inside the LocalBroadcastManager source code , you will see that it is really a regular 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", therefore, NO: if the application is killed, the LocalBroadcastManager class is killed with it, and the leak will not occur .

+4
source

You must completely cancel it in onDestroy, because onDestroy does not mean that your application is completed - it is just your activity. Other actions in your application may still work, so the receiver may cause a leak.

0
source

Source: https://habr.com/ru/post/1488552/


All Articles