weixin_33725126 2016-08-06 16:04 采纳率: 0%
浏览 31

AJAX相关的内存泄漏

I'm using jQuery 3.1.0 and UWamp 3.1.0 to run this script.

I want to create a script that can connect to a server in order to execute a chain of repetitive tasks; that server doesn't belong to my domain.

I'll post the code only starting from the AJAX calls, as i think they're the ones causing the leaks

This first part connects to a precalculated link and downloads a list of other links to connect to (parser simply returns the content of the given URL).

$.post("php/parser.php", {
    url: uri
}, function(res, status) {
    // Parsing

The response always contains multiple links, and i need to connect to the first one, execute a few more calls, and only then i can proceed to the second, then the third, etc.
The links are stored in the array urls.

    // Connects to the first link in the list
    console.log("Request to: " + urls[0]);
    urls[0] = $.post("php/parser.php",
        {
            url: urls[0]
        },
        function(res, state) {
            console.log(0 + ": completed");
        }
    );
    for(var y = 0; y < urls.length;/*y++*/) {
        $.when(urls[y]).done(function(res) {
            // Other calls here
            console.log("Doing stuff");
            // Continuing with the next link
            y++;
            if(y < urls.length) {
                console.log("Request to: " + urls[y]);
                urls[y] = $.post("php/parser.php",
                    {
                        url: urls[y]
                    },
                    function(res, state) {
                        console.log(y + ": completed");
                    }
                );
            }
        }, function(res) {});
    }
});

Now: On Chrome/IE only the first two calls will be executed, since $when(urls[y]).done() is never triggered, as urls[0].state() always equals pending.

On Firefox, if i debug the code everything runs smoothly (spam pressing f10 to step to the next instruction brings an execution time of 6s), whilst if i let the code run on its own, several problems will happen, ranging from console.log() not showing messages, to the browser taking hundreds of megabites out of my RAM each second (whilst progressing extremely slowly), to the point of crashing the browser after about 30s.
A little more precise data, taken from Task Manager; when not executing the script, Firefox uses 480MB of RAM:
debug->up to 500MB of RAM in 6s
run->up to 7GB of RAM in 30s
(I have a lot of other tabs open, if it changes anything)

I'm a complete newbie about js/jQuery/php, and looking online hasn't helped me find any solution; if anyone a little more experienced could spend a little time looking into this, it'd be greatly appreciated. Thanks in advance.

Edit: i solved the problem by making all the calls synchronous, but i'd still be interested in finding the reason the leak happens

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题
    • ¥20 求一个html代码,有偿