android – 在Chrome自定义标签中检测到内存泄漏

前端之家收集整理的这篇文章主要介绍了android – 在Chrome自定义标签中检测到内存泄漏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图实现Chrome自定义标签,并通过LeakCanary检测内存泄漏.

演示应用程序似乎不会泄漏,除非我们添加另一个Activity层(即MainActivity启动Activity2,将绑定/取消绑定到自定义选项卡服务并启动url – MainActivity在demo app中的所有内容).

MainActivity看起来像这样:

  1. public class MainActivity extends Activity implements OnClickListener {
  2. private Button mLaunchButton;
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7.  
  8. LeakCanary.install(getApplication());
  9.  
  10. setContentView(R.layout.main);
  11.  
  12. mLaunchButton = (Button) findViewById(R.id.launch_button);
  13. mLaunchButton.setOnClickListener(this);
  14. }
  15.  
  16. @Override
  17. public void onClick(View v) {
  18. int viewId = v.getId();
  19.  
  20. if (viewId == R.id.launch_button) {
  21. Intent intent = new Intent(getApplicationContext(),Activity2.class);
  22. startActivity(intent);
  23. }
  24. }
  25. }

从Activity2返回到MainActivity将导致此泄漏:

  1. 09-04 13:49:26.783 10456-12161/org.chromium.customtabsclient.example D/LeakCanary In org.chromium.customtabsclient.example:1.0:1.
  2. 09-04 13:49:26.783 10456-12161/org.chromium.customtabsclient.example D/LeakCanary * org.chromium.customtabsclient.Activity2 has leaked:
  3. 09-04 13:49:26.783 10456-12161/org.chromium.customtabsclient.example D/LeakCanary * GC ROOT android.support.customtabs.CustomTabsClient$1.val$callback (anonymous class extends android.support.customtabs.ICustomTabsCallback$Stub)
  4. 09-04 13:49:26.783 10456-12161/org.chromium.customtabsclient.example D/LeakCanary * references org.chromium.customtabsclient.Activity2$2.this$0 (anonymous class extends android.support.customtabs.CustomTabsCallback)
  5. 09-04 13:49:26.783 10456-12161/org.chromium.customtabsclient.example D/LeakCanary * leaks org.chromium.customtabsclient.Activity2 instance

https://gist.github.com/abvanpelt/ddbc732f31550b09fc27

我的问题是:这是演示应用程序中的错误吗? (可能unbindCustomTabsService()缺少一些需要的拆卸?)或者这是Chrome自定义选项卡库中的错误

谢谢.

解决方法

示例中的MainActivity将创建一个CustomTabsServiceConnection和CustomTabsCallback作为匿名内部类的实例.

如果将其更改为静态内部类,那么删除对MainActivity的引用,并将MainActivity的引用设置为WeakReferences,您将看到LeakCanary停止关于MainActivity泄露的报告.

现在,如果您将其设置为观看该对象,则仍可能会看到有关ServiceConnection漏洞的漏洞报告.原因是它链接到Chrome服务,GC不能在GC服务器端运行.

我创建了一个在一个循环中绑定和解除绑定的测试,我已经确认ServiceConnections确实在一段时间后被收集.

因此,可以改进演示,以避免ServiceConnection对MainActivity的引用,避免在服务断开连接后长时间存在活动等重物,而且自定义标签库也不是问题.

猜你在找的Android相关文章