在分析我的应用程序时,我发现,使用
nsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE); // LEAKS!正在泄露活动,而
nsdManager = (NsdManager) getApplicationContext().getSystemService(Context.NSD_SERVICE);不会的。我制作了一个小型测试应用程序,它只会使用活动上下文检索NsdManager,但它仍然在泄漏活动。这是个虫子吗?我真的必须一直使用应用程序上下文吗?
设备:模拟器(Android11,API 30,x86)
发布于 2022-06-28 16:06:08
此问题与获取WIFI_SERVICE时出现的内存泄漏问题类似。
在做时:
activity.getSystemService(Context.WIFI_SERVICE);显示了以下警告。
The WIFI_SERVICE must be looked up on the
Application context or memory will leak on
devices < Android N. Try changing activity
to activity.getApplicationContext().虽然在使用NSD_SERVICE时没有林特警告,但由于我看到内存泄漏引用了NsdManager,所以使用:
activity.getSystemService(Context.NSD_SERVICE);我认为使用:
activity.getApplicationContext().getSystemService(Context.NSD_SERVICE);似乎是正确的行动。
https://stackoverflow.com/questions/64900713
复制相似问题