如何在 IE 中强制 Javascript 垃圾收集?AJAX 调用和 DOM 操作后 IE 的运行速度非常慢

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2760285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:48:38  来源:igfitidea点击:

How can I force Javascript garbage collection in IE? IE is acting very slow after AJAX calls & DOM manipulation

javascriptjqueryinternet-explorer

提问by aw crud

I have a page with chained drop-downs. Choosing an optionfrom the first selectpopulates the second, and choosing an optionfrom the second selectreturns a table of matching results using the innerHtmlfunction on an empty divon the page.

我有一个带有链接下拉列表的页面。option从第一个中选择一个select填充第二个,option从第二个中选择一个select使用页面innerHtml上的空白处的函数返回匹配结果表div

The problem is, once I've made my selections and a considerable amount of data is brought onto the page, all subsequent Javascript on the page runs exceptionally slowly. It seems as if all the data I pulled back via AJAX to populate the divis still hogging a lot of memory. I tried setting the return object which contains the AJAX results to nullafter calling innerHtmlbut with no luck.

问题是,一旦我做出选择并将大量数据带入页面,页面上所有后续 Javascript 的运行速度就会异常缓慢。似乎我通过 AJAX 撤回以填充的所有数据div仍在占用大量内存。我尝试null在调用后将包含 AJAX 结果的返回对象设置为,innerHtml但没有运气。

Firefox, Safari, Chrome and Opera all show no performance degradation when I use Javascript to insert a lot of data into the DOM, but in IE it is very apparent. To test that it's a Javascript/DOM issue rather than a plain old IE issue, I created a version of the page that returns all the results on the initial load, rather than via AJAX/Javascript, and found IE had no performance problems.

Firefox、Safari、Chrome 和Opera 在使用Javascript 向DOM 中插入大量数据时都没有表现出性能下降,但在IE 中则非常明显。为了测试这是一个 Javascript/DOM 问题而不是一个普通的旧 IE 问题,我创建了一个页面版本,在初始加载时返回所有结果,而不是通过 AJAX/Javascript,发现 IE 没有性能问题。

FYI, I'm using jQuery's jQuery.get method to execute the AJAX call.

仅供参考,我使用 jQuery 的 jQuery.get 方法来执行 AJAX 调用。

EDITThis is what I'm doing:

编辑这是我在做什么:

<script type="text/javascript">
function onFinalSelection() {
  var searchParameter = jQuery("#second-select").val();
  jQuery.get("pageReturningAjax.php",
  {SEARCH_PARAMETER: searchParameter},
  function(data) {
    jQuery("#result-div").get(0).innerHtml = data;
   //jQuery("#result-div").html(data); //Tried this, same problem
    data = null;
  },
  "html");
}
</script>

I want to note that this only becomes an issue when the return datais quite large. It is directly related to the size, as I am able to see moderate slowdown for medium size results and only major slowdown when it is a few hundred records + being returned.

我想指出的是,只有当回报data相当大时,这才会成为一个问题。它与大小直接相关,因为我能够看到中等大小的结果适度放缓,只有在返回几百条记录时才会出现明显的放缓。

采纳答案by David Murdoch

Use

$("#result-div").html(data);

html()utilizes jQuery's emptymethod which works very hard to prevent memory leaks.

html()利用 jQuery 的empty方法,该方法非常努力地防止内存泄漏。

have you tried:

你有没有尝试过:

delete data;


I'm thinking there are other performance issues in your code causing the sluggishness. Is your return data using png's with alpha transparency? I've seen that kill IE6 (when the alpha filter is applied) and slow down IE7 considerably.

我认为您的代码中还有其他性能问题导致了缓慢。您的返回数据是否使用具有 alpha 透明度的 png?我已经看到杀死 IE6(当应用了 alpha 过滤器时)并大大降低了 IE7 的速度。

回答by ignaZ

You can force garbage collection in IE by using the CollectGarbagefunction, e.g.

您可以使用CollectGarbage函数在 IE 中强制垃圾收集,例如

if (typeof(CollectGarbage) == "function")
    CollectGarbage();

The JScript garbage collector is described in detail in this blog entry: http://blogs.msdn.com/ericlippert/archive/2003/09/17/53038.aspx

JScript 垃圾收集器在这个博客条目中有详细描述:http: //blogs.msdn.com/ericlippert/archive/2003/09/17/53038.aspx

As the blog says, the GC is not predictable, so delete dataor data = nullwill notreclaim the memory immediately, but it eventually will reclaim it.

随着博客称,GC是不可预测的,所以删除数据数据= NULL不会立即回收内存,但它最终将收回它。



But I doubt that your performance penalty is really caused by the memory usage; I think that it is a problem with DOM rendering.

但我怀疑您的性能损失是否真的是由内存使用引起的;我认为是DOM渲染的问题。

回答by Max

If somebody interested not only in IE:

如果有人不仅对 IE 感兴趣:

To force garbage collection in Gecko:

在 Gecko 中强制垃圾收集:

window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  .getInterface(Components.interfaces.nsIDOMWindowUtils)
  .garbageCollect();

Link

关联