解释Chrome任务管理器中的内存使用情况 [英] Interpretation of memory usage in chrome task manager

查看:376
本文介绍了解释Chrome任务管理器中的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个大型Java脚本应用程序中的内存泄漏调查开始,该应用程序在基于webkit和sfx(Java脚本核心引擎)的浏览器中作为小部件运行.一段时间后,我发现内存的来源随着时间的推移而增加.碰巧是$.ajax(..).

I started from memory leak investigation in a large java script app which is run as a widget in a browser based on webkit and sfx (java script core engine). After a while I found a source of memory increasing over time. It happened to be $.ajax(..).

好的,我想,让我们尝试使用不同版本的jquery或直接使用XMLHttpRequest-相同的结果.然后我在google chrome上检查了它,感到很惊讶,因为堆分析器既未显示js对象数量的增加,也未显示堆大小的增加,而chrome任务管理器未显示内存和私有内存的增加.我测试的代码如下:

Okay, I thought, let's try different versions of jquery or use XMLHttpRequest directly - same result. Then I checked it on google chrome and was surprised, because heap profiler showed neither increase of number of js objects nor increase of heap size whereas chrome task manager showed increase of memory and private memory. The code I tested is as follows:

var Main = {};
var xhr = new XMLHttpRequest();

function makeRequest() {
    xhr.timeout = 2000;
    xhr.ontimeout = function() {
        xhr.abort();
    }
    xhr.onreadystatechange = function() {
        if (xhr.readyState != 4) return;
    };

    xhr.open("POST", "", true); //async post request to local server
    xhr.send("{}");
}

Main.onLoad = function()
{
    setInterval(function() { makeRequest(); }, 3000);
}

那么,这种行为可以吗?这不是本机内存泄漏的迹象吗?

So, is this behavior ok? Isn't it a sign of native memory leak?

P.S.我在Windows 7企业版上使用的Chrome版本为35.0.1916.153.

P.S. I use chrome of version 35.0.1916.153 on windows 7 enterprise.

推荐答案

根据规范,则xhr不会自动GC.

According to the spec, the xhr isn't automatically GC'd.

4.2垃圾收集

4.2 Garbage collection

如果XMLHttpRequest对象的状态为,则不能对其进行垃圾回收 OPENED并设置send()标志,其状态为HEADERS_RECEIVED,或者 其状态为LOADING,并且已注册一个或多个事件侦听器 其类型是readystatechange,progress,abort,error,load, 超时并加载.

An XMLHttpRequest object must not be garbage collected if its state is OPENED and the send() flag is set, its state is HEADERS_RECEIVED, or its state is LOADING and it has one or more event listeners registered whose type is one of readystatechange, progress, abort, error, load, timeout, and loadend.

如果XMLHttpRequest对象在连接时被垃圾回收 仍处于打开状态,则用户代理必须终止请求.

If an XMLHttpRequest object is garbage collected while its connection is still open, the user agent must terminate the request.

本文对此有很好的解释问题和类似的.通常的解决方法是在xhr.send()之后通过xhr = null清除回调中对主机对象的引用.我说通常的修复方法是因为在此示例中,您不会分配更多的xhr对象或对结果进行任何处理.如您所说,堆没有变化,看起来也不像是在泄漏.

This article has a good explanation of this problem and similar ones. The usual fix is to clear references to the host object in the callbacks via xhr = null after xhr.send(). I say usual fix because you aren't allocating more xhr objects or doing anything with the results in this example. As you said, the heap isn't changing and it doesn't look like you have a leak.

仅使用chrome任务管理器无法告诉您是否存在泄漏.我发现本文作为有关如何使用chrome的devtool进行诊断的有用教程内存泄漏.您将需要手动触发GC并确认内存没有持续增长.如果发现确实存在内存泄漏,则可以使用devtools找出哪些对象不是保留路径的一部分,而是保留在内存中的(该文章中有关如何执行此操作的更多信息).

Using chrome task manager alone can't tell you if you have a leak. I found this article as a helpful tutorial on how to use chrome's devtools to diagnose memory leaks. You will need to trigger GC manually and verify the memory doesn't keep growing. If you find you do indeed have a memory leak, you can use devtools to figure out which objects are are not part of the retaining path, but still in memory (more information about how to do this in that article).

这篇关于解释Chrome任务管理器中的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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