JavaScript 内存泄漏

发布于 2024-11-28 00:35:39 字数 761 浏览 0 评论 0原文

我刚刚注意到我刚刚编写的一些 javascript 似乎正在泄漏内存,它是一段非常简单的代码 - 感谢 jquery - 但我可以看到它在任务管理器中运行,并且内存使用量正在慢慢增加 4 之间和 40 字节。

我所做的就是通过 getJSON 在 asp mvc 控制器/操作中抛出一些数据:

$(document).ready(function () {
    var olddata = "";
    window.setInterval(function () {
        var options = JSON.stringify({
            orderby: "name"
        });
        var params = {
            options: options,
            data: olddata ? JSON.stringify(olddata) : ""
        };
        $.getJSON("/Home/GetTasks", params, function (json) {
            olddata = json;
            json = null;
        });
        params = null;
        options = null;
    }, 1000);
});

我调高了计时器值只是为了更容易地看到问题。我显然在这里做错了什么,但看不出是什么。

我应该清理 getJSON 调用吗?

TIA。

i've just noticed that some javascript i;ve just written appears to be leaking memory, it quite a simple piece of code - thanks to jquery - but I can watch it running in taskmanager and the memory usage is slowly clicking up by between 4 and 40 bytes.

All i'm doing is throwing some data at a asp mvc controller/action via getJSON:

$(document).ready(function () {
    var olddata = "";
    window.setInterval(function () {
        var options = JSON.stringify({
            orderby: "name"
        });
        var params = {
            options: options,
            data: olddata ? JSON.stringify(olddata) : ""
        };
        $.getJSON("/Home/GetTasks", params, function (json) {
            olddata = json;
            json = null;
        });
        params = null;
        options = null;
    }, 1000);
});

I've bumped up the timer value just to see the problem more readily. I'm obviously doing something wrong here butcan't see what.

Should I be cleaning up the getJSON call?

TIA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-12-05 00:35:39

你怎么知道你实际上正在泄漏内存?

对于 4 和 40 字节等较小的数字,您可能只会看到堆增长,但堆中的一些新块是“空闲”的并且可供将来使用,因此虽然应用程序总体内存使用量增长,但内存实际上并没有增长。泄漏并且可供将来使用,因此它不会永远增长。

如果这是您实验的全部范围,那么我认为代码没有任何问题。

这里有三个函数闭包。 $(document).ready() 闭包会持续代码的生命周期,但这只是一次性交易,因此应该没有问题。

传递给 setInterval() 的匿名函数使 $(document).ready() 闭包保持活动状态。对 setInterval() 匿名函数的每次调用都应该是一次新的调用,它将获取一组新的局部变量,并在先前的调用运行完成时释放旧的变量。

传递给 getJSON() 的匿名函数在 setInterval 匿名函数上创建一个闭包,但该闭包只能持续到 getJSON 函数完成以及执行 setInterval() 时匿名函数闭包应该被释放。

我看到在这里持续存在的唯一闭包是 $(document).ready() 闭包,这是您想要的,并且它只创建一次,因此不会导致泄漏。

getJSON 匿名函数中的所有局部变量都将在其完成时被释放。 getJSON 调用中唯一保留下来的数据是您的分配:

olddata = json;

但是,每个连续的分配只是替换前一个调用中的数据,因此以前的数据不再被引用并且可供垃圾收集器回收。

这里没有 DOM 操作,因此 DOM 和 JS 之间没有交叉或循环引用的机会。

我的结论是我没有看到任何会泄漏的东西。我看到很多东西都使用临时内存,所以我怀疑您在进程内存使用中看到的只是堆增长,但增长的方式是增长的内存最终会被重用。如果浏览器还缓存 JSON 结果,您也可能会看到内存缓存增长。

不幸的是,在当今的浏览器中,很难判断什么时候是真正的内存泄漏,什么时候是缓存、通用堆等使用的浏览器内存的临时扩展……在极端情况下,您可以将所有缓存设置得非常小并运行此命令很长一段时间(数十万次迭代)。如果不是泄漏,内存使用最终应该会趋于平稳。如果是泄漏,内存使用量应该会继续相对线性增长。

免责声明:这里的一个免责声明是,我假设 jQuery 函数 $.getJSON() 不会泄漏自身,并且总是以一种清理即使 ajax 调用不成功,它也会创建闭包。

How do you know you're actually leaking memory?

At small numbers like 4 and 40 bytes, you could just be seeing heap growth, but some of the new blocks in the heap are "free" and available for future use so while the overall app memory use grows, the memory isn't actually leaking and will be available for future use so it won't grow forever.

If this is the entire extent of your experiment, then I don't see any issues with the code.

There are three function closures here. The $(document).ready() closure lasts the lifetime of your code, but it's just a one-time deal so there should be no issue.

The anonymous function passed to setInterval() keeps the $(document).ready() closure alive. Each call to the setInterval() anonymous function should be a new call that will get a new set of local variables and release it's old ones when the prior calls runs to completion.

The anonymous function passed to getJSON() creates a closure on the setInterval anonymous function, but that closure should only last until the getJSON function finishes and when it does the setInterval() anonymous function closure should be released.

The only closure that I see that lasts here is the $(document).ready() closure which is something you intend and it's only created once so it should cause no leak.

All the local variables in the getJSON anonymous function are going to be released when the it finishes. The only data from the getJSON call that survives is your assignment of:

olddata = json;

But, each successive assignment is just replacing the data from the previous call so the previous data is no longer referenced and available for recycling by the garbage collector.

There are no DOM manipulations here so there is no opportunity for cross or circular references between DOM and JS.

My conclusion is that I don't see anything that will leak. I see plenty of things using temporary memory so I suspect what you're seeing in the process memory usage is just heap growth, but growth in a way that the memory that has grown will eventually get reused. If the browser is also caching the JSON results, you could be seeing memory cache growth too.

Unfortunately, in today's browsers, it's difficult to tell when it's really a memory leak versus a temporary expansion of browser memory used by caching, general heaps, etc... In the extreme, you could set all caches to be very small and run this for a long time (hundreds of thousands of iterations). If it's not a leak, memory use should eventually flatten out. If it is a leak, memory usage should continue to grow relatively linearly.

Disclaimer: the one disclaimer here is that I'm assuming that the jQuery function $.getJSON() doesn't leak itself and always finishes in a way that cleans up the closure it creates, even if the ajax call is not successful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文