了解javascript闭包和内存使用 [英] Understanding javascript closures and memory usage

查看:89
本文介绍了了解javascript闭包和内存使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT :这只是一个简单的例子来说明我对一个更大的程序的关注。我不会使用这个实际代码的任何东西:)

EDIT: This is just a simple example to demontrate the concern I have with a much larger program. I wouldn't use this actual code for anything :)

如果我运行 -

<!DOCTYPE html>
<html>
<head>
<script>

function update(amount, win, data)
{
    win.innerText = 'Count is ' + amount;
    setTimeout(function() { update(amount + 1, win, {data: 'something'})}, 1000);
}

window.onload = function() {

  var win = document.getElementById('item');    
  update(0, win, 0);
}
</script>
</head>

<body>
<div id="item"></div>
</body>
</html>

调用setTimeout可能会创建一个闭包,它将参数的内容捕获到更新函数(金额,赢,数据)。所以这些变量保存在内存中,直到超时被调用并返回,以便它们在函数调用内可用...

The call to setTimeout presumably creates a closure which captures the contents of the parameters to the "update" function (amount, win, data). So those variables are maintained in memory until the timeout is called and returns so that they will be available inside that function call...

但是该函数创建一个新的闭包下一次迭代的超时...在第二个闭包将捕获什么?它只是这些变量的副本,还是形成函数调用的那些副本会在新闭包中再次捕获

But that function creates a new closure for the next iteration of the timeout... What will be captured in that second closure? Is it just the new copies of those variables or will the ones that formed part of the function call be captured again in the new closure?

基本上,由于每个闭包中的数据越来越大,或者这是安全合理的,这最终会耗尽内存吗?

Basically will this eventually run out of memory due to the data in each closure getting bigger and bigger, or is this safe and reasonable?

推荐答案

在我的理解中,当一个闭包创建时,当前的词汇上下文是捆绑在一起的。在你的情况下,它将是 amount,win,data

In my understanding, when a closure is created, the current lexical context is bundled with it. In your case, it would be the amount, win, data.

超时触发,执行闭包,从而再次调用 update ;这个调用虽然可能出现,但是不是递归的,因为 update 的先前执行已经结束,并且其原始上下文(动态的,不同于词汇的)已经被释放。 (我认为这是很重要的注意,因为它似乎你担心一种堆栈增长由于递归)。

This context will be used, when the timeout fires, to execute the closure and thus call once again the function update; this call, although it might appear so, is not recursive, because the previous execution of update already ended and its original context (dynamic, which is different from the lexical one) has already been freed. (I think this is important to notice, because it seems you are worrying about a sort of stack growth due to recursion).

再次, update 第二次执行,并且再次设置超时并创建闭包。这个闭包与当前的执行语句上下文(它仍然包括 amount,win,data )捆绑在一起,并用计时器计划。然后 update 完成并从堆栈中删除。那么再次调用定时器并更新...

So, again, update is executed a second time and again a timeout is set and a closure created. This closure is bundled with the current lexical context of execution (which still includes just amount, win, data) and scheduled with the timer. then update finishes and removed from the stack. then again the timer fires and update is called again...

因此,您不必担心上下文的无限增长,原因有两个:首先,词汇语境与闭包捆绑在一起;该调用实际上不是递归的。

So, you should not worry about an unlimited growth of the context, for two reasons: first, only the lexical context is bundled with the closure; the call is not actually recursive.

这篇关于了解javascript闭包和内存使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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