While profiling my app, I found, that retrieving the NsdManager by using
nsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE); // LEAKS!
is leaking the Activity, while
nsdManager = (NsdManager) getApplicationContext().getSystemService(Context.NSD_SERVICE);
does not. I made a small test app, that does nothing else with the NsdManager than retrieving it using the Activity context, but it's still leaking the activity. Is this a bug? Do I really have to use the application context all the time?
Device: Emulator (Android 11, API 30, x86)
This problem seems similar to memory leak issues with obtaining WIFI_SERVICE.
When doing:
Android Studio lint shows the following warning.
Although there is no lint warning with NSD_SERVICE and because I see memory leaks referencing NsdManager when using:
I think using:
seems like the correct action.