Interpretation of memory usage in chrome task manager

做~自己de王妃 提交于 2019-12-06 07:10:43

问题


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(..).

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. I use chrome of version 35.0.1916.153 on windows 7 enterprise.


回答1:


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

4.2 Garbage collection

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.

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

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.

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).



来源:https://stackoverflow.com/questions/24756853/interpretation-of-memory-usage-in-chrome-task-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!