Android-具有指向WebView的指针的Javascript接口-如何在不使用memleak的情况下实现它 [英] Android - Javascript Interface with pointer to WebView - how to achive it without memleak

查看:55
本文介绍了Android-具有指向WebView的指针的Javascript接口-如何在不使用memleak的情况下实现它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我如何在Android应用程序中将JSInterface添加到Webview(我们在扩展的WebView类内部):

  addJavascriptInterface(new JSInterface(this),"appBridge");//'this'是我们位于其中的MyWebView 

然后,构造函数JSInterface将"this"存储到他的受保护变量中,因为JSInterface方法需要访问WebView类.

但是这种方式似乎会导致内存泄漏.MyWebView(以编程方式创建)具有指向JSInterface(addJavascriptInterface)的指针,而JSInterface具有指向WebView的指针.因此,垃圾收集器无法将其从内存中删除.我说的对吗?

解决方案

WebView将保留您的对象(JSInterface的实例),直到您调用 removeJavascriptInterface("appBridge")并重新加载页面./p>

但是对于标记紧凑的收集器(用于带有内置垃圾收集的语言,例如Java或JavaScript),这种循环引用实际上不是问题,因为一旦这样的循环构造就没有任何引用从外部(也就是说,您已经删除了对WebView实例的最后一个引用),它将被收集为不可访问:

  app --->WebView->JSInterface |WebView和JSInterface均可访问^ |--------------/应用-/->WebView->JSInterface |现在,这种环状结构/^ ||无法到达,将被收集-------------- 

您可能会将其与引用计数(通常在C ++等语言中使用)混淆,实际上,从一个对象到另一个对象的相互硬引用将导致它们永久保留.

还有一个旁注-如果您关心内存使用情况,请不要在不需要并将其从视图层次结构中删除后立即调用 WebView.destroy()释放本机代码使用的资源.

This is how I add JSInterface to Webview in Android app (we are inside of extended WebView Class):

addJavascriptInterface(new JSInterface(this), "appBridge"); // 'this' is MyWebView that we are inside

Then constructor JSInterface stores 'this' to his protected variable, because JSInterface methods need access to WebView class.

But this way seems to lead to memory leaks. The MyWebView (created programatically) has pointer to JSInterface (addJavascriptInterface) and JSInterface has pointer to WebView. So they can't be removed from memory by Garbage Collector. Am I right?

解决方案

WebView will hold your object (the instance of JSInterface) until you call removeJavascriptInterface("appBridge") and reload your page.

But this kind of a circular reference isn't actually a problem for a mark-compact collector (used in languages with built-in garbage collection, like Java or JavaScript), because once such looped construction doesn't have any references from the outside (that is, you have eliminated your last reference to the WebView instance), it will be collected as unreachable:

app ---> WebView --> JSInterface  | both WebView and JSInterface are reachable
           ^            |
           --------------

      /
app -/-> WebView --> JSInterface  | now this looped construction
    /      ^            |         | is unreachable and will be collected
           --------------

You are probably confusing this with reference counting (which is often used in languages like C++), where indeed mutual hard references from one object to another will cause them to remain forever.

And a side note -- if you care about your memory usage, don't forget to call WebView.destroy() once you don't need it and have removed it from the views hierarchy to free up resources used by native code.

这篇关于Android-具有指向WebView的指针的Javascript接口-如何在不使用memleak的情况下实现它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆