简体   繁体   English

GWT垃圾收集

[英]GWT Garbage Collection

I am creating a new presenter like so: 我正在创建一个新的演示者:

new MyPresenter(new MyView());

It registers some event handlers and binds to the view and such. 它注册了一些事件处理程序并绑定到视图等。 Eventually, I might "close" that view so that it is no longer rendered by the browser. 最终,我可能会“关闭”该视图,以便浏览器不再呈现它。 I am not maintaining a reference to this instance of MyPresenter anywhere. 我没有在任何地方维护对MyPresenter这个实例的MyPresenter

In the Google forums on this topic, the conventional response is to "set your references to null" and then don't worry about it. 在关于此主题的Google论坛中,传统的响应是“将引用设置为null”,然后不要担心它。 Unlike in Javascript, I can't just say this = null; 与Javascript不同,我不能只说this = null; in Java for obvious reasons. 在Java中有明显的原因。 But in Javascript, it's very easy to null out object references that I know will no longer be used. 但是在Javascript中,很容易找出我知道将不再使用的对象引用。

My question: How can I tell if this presenter has been garbage collected since I don't maintain a reference to it? 我的问题:我如何判断这位演示者是否已被垃圾收集,因为我没有保留对它的引用? It very clearly exists. 它显然存在。 Should I have faith that GWT and JS will take care of this? 我是否应该相信GWT和JS将会解决这个问题? Or do I need to maintain my own reference to MyPresenter so that I can manually null it when I'm done with it? 或者我是否需要维护自己对MyPresenter的引用,以便在完成后可以手动将其置null

There are two kinds of memory leaks: 有两种内存泄漏:

  • DOM/browser level memory leaks DOM /浏览器级内存泄漏
  • Application memory leaks. 应用程序内存泄漏。

DOM/browser level memory leaks typically last after you close the app. 关闭应用程序后,DOM /浏览器级内存泄漏通常会持续存在。 AFAIK only old browsers (IE6) are affected and that's the reason why GWT uses a special way to attach handlers. AFAIK仅旧浏览器(IE6)受到影响,这就是GWT使用特殊方式附加处理程序的原因。
This should be a non issue with modern browsers at least they will be a non-issue if you close the app. 对于现代浏览器而言,这应该是一个非问题,如果您关闭应用程序,至少它们将不是问题。 They can however become application memory leaks. 然而,它们可能成为应用程序内存泄漏。 (See here for more details). (有关详细信息,请参见此处 )。 But in general modern Javascript GC are pretty good in freeing unused memory. 但总的来说,现代Javascript GC在释放未使用的内存方面相当不错。

Application memory leaks might be an issue with long running applications and when you dynamically create a lot of views/presenters and keep reference via Eventhandlers. 应用程序内存泄漏可能是长时间运行的应用程序以及动态创建大量视图/演示者并通过Eventhandler保持引用时的问题。 But here it really depends on the scope of the involved parties. 但这里真的取决于有关各方的范围。
This post is a good reference with some more infos on that. 这篇文章是一个很好的参考,有更多的信息。

Finally to make sure that you really don't have any application memory leaks you should use the Dev Tools Heap Profiler to check the memory consumption over a longer period. 最后,为了确保您确实没有任何应用程序内存泄漏,您应该使用Dev Tools Heap Profiler来检查较长时间内的内存消耗。
This blog post has some more infos on that. 这篇博文有更多的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM