关于 Javascript Windows Gadget 内存泄漏的问题(也是一般情况)
我曾在 Windows 7 Gadget 的内存泄漏问题上工作了一段时间,然后实现了更多的功能,我相信这些功能使内存泄漏变得更糟/增加了更多的泄漏。我运行了该小工具的 4 个实例大约 16 小时,内存使用量从 22,000K 增加到 36,000K。然而,令我不安的是句柄、USER 对象和 GDI 对象仍然大致相同。 USER 和 GDI 对象实际上保持完全相同,并且句柄一直从 1026 跳到 1034。我认为这只是侧边栏的一些怪癖,因为我的每个小工具每 7 秒刷新一次。
我有两个问题,一般情况下,什么样的内存泄漏会导致这样的情况?就我而言,我使用 Ajax 来访问此小工具的 Web 服务,因此如果有人知道在 Ajax 中可能导致此类问题的原因,那就太好了。
我已经看过有关 Javascript 内存泄漏主要原因的文章,我只是想知道是否有人知道为什么当句柄和对象保持有效相同时内存会增加这么多。对于这种情况,也许 Ajax 或一般编程中有已知的原因?
I was working on memory leaks for a Windows 7 Gadget for awhile, and then got more functionality to implement which I believe made the memory leaks worse/added more leaks. I ran 4 instances of the gadget for roughly 16 hours and memory usage went from 22,000K to 36,000K. However, something that perturbed me is that the Handles, USER Objects, and GDI Objects remained roughly the same. The USER and GDI Objects actually remained exactly the same and the Handles jumped from something like 1026 to 1034 in all that time. I consider that to just be some eccentricity of sidebar since each of my gadgets refresh every 7 seconds.
I have two questions, in the general case, what kind of memory leaks cause something like this? And in my case I use Ajax to access web services for this gadget, so if anyone knows what might cause such a thing in Ajax that would be great.
I have already seen articles on main causes of memory leaks in Javascript, I'm just wondering if anyone has any ideas on why the memory would go up so much when the handles and objects stayed effectively the same. Perhaps there are known causes in Ajax or programming in general for such a case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我非常欣赏在没有库的情况下完成的 JS 工作,但在这种情况下,您可能想将 JQuery(或 Prototype、Mootols 等)加入其中。它们针对 Ajax 请求(和 DOM 操作)的内存消耗进行了很好的测试。
您首先不需要修改 DOM 代码,而是尝试用 JQuery 的 Ajax 方法替换 Ajax 请求。然后再次运行内存测试。如果这消除了它,那么您就知道 XHR 代码中存在内存错误。
否则,它可能与销毁元素之前未正确删除 DOM 代码中的事件处理程序有关(我假设您在某个时刻删除并添加了 DOM 元素)。如果没有在 IE 中删除处理程序,则会造成内存泄漏(DOM 元素无限期地保存在内存中,糟糕)。
I definitely appreciate JS work done without libraries but in a situation like this, you may want to toss JQuery (or Prototype, Mootols, etc) into the mix. They are very well tested for memory consumption with Ajax requests (and DOM manipulation).
You don't need to modify your DOM code at first but try replacing your Ajax requests with JQuery's Ajax methods. Then run your memory tests again. If that eliminates it, you know you've got a memory bug in your XHR code.
Otherwise, its probably related to event handlers in your DOM code not being properly removed before you destroy an element (I'm assuming you remove and add DOM elements at some point). If a handler isn't removed in IE, that will create a memory leak (the DOM element is held in memory indefinitely, suck).
Windows 7 64 位 SP1 引入了 sidebar.exe 内存泄漏,您可能会受到影响。据我所知,这个问题还没有得到解决,但是 这篇博文提供了一种解决方法。
Windows 7 64bit SP1 introduced sidebar.exe memory leak, which you might be affected with. As far as I know, this has not been fixed yet, however this blog post offers a workaround.