Javascript 中同一变量上没有“delete”的“new”

发布于 2024-10-15 03:36:46 字数 767 浏览 2 评论 0原文

这样做可以吗?:

function mygetTime()
{
    var d = new Date();
    return(d.getTime());
}

function wasteSomeMemory()
{
    var temp;
    for(var count = 0; count < 1000000; count += 1)
    {
        temp = mygetTime();
    }
}

调用wasteSomeMemory()会导致内存泄漏吗?

怎么样:

function wasteSomeMemory2()
{
    var temp;
    for(var count = 0; count < 1000000; count += 1)
    {
        temp = new Date();
    }
}

调用wasteSomeMemory2()会导致内存泄漏吗?我应该在 for 循环末尾使用 delete temp; 吗?

function wasteSomeMemory2()
{
    var temp;
    for(var count = 0; count < 1000000; count += 1)
    {
        temp = new Date();
        delete temp;
    }
}

Is it OK to do this?:

function mygetTime()
{
    var d = new Date();
    return(d.getTime());
}

function wasteSomeMemory()
{
    var temp;
    for(var count = 0; count < 1000000; count += 1)
    {
        temp = mygetTime();
    }
}

Will calling wasteSomeMemory() cause a memory leak?

What about this:

function wasteSomeMemory2()
{
    var temp;
    for(var count = 0; count < 1000000; count += 1)
    {
        temp = new Date();
    }
}

Will calling wasteSomeMemory2() cause a memory leak? Should I use delete temp; at the end of the for-loop?

function wasteSomeMemory2()
{
    var temp;
    for(var count = 0; count < 1000000; count += 1)
    {
        temp = new Date();
        delete temp;
    }
}

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

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

发布评论

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

评论(2

酒废 2024-10-22 03:36:46

newdelete 在 JavaScript 中彼此没有任何关系(尽管它们与其他语言中完全不同的结构有着令人困惑的相似性)。不要担心创建对象(new)而不显式清理它们,这是垃圾收集器的工作。

new 用于通过构造函数创建对象。另一方面,delete 用于从对象中删除属性。它与从内存中删除对象没有任何关系,除了作为副作用(例如,如果对该对象的唯一未完成引用来自您删除的属性)。

正确使用delete的示例:

var obj = {};
obj.foo = "bar"; // Now `obj` has a property called `foo`
delete obj.foo;  // Now it doesn't

您的getmyTime函数完全没问题。 Date 对象将在函数返回后立即有资格被回收(是否被回收完全取决于实现)。它不会导致内存泄漏,除非有错误的实现。

您的wasteSomeMemory2同样不会导致内存泄漏,事实上您不能调用delete temp; ——您只能删除属性,而不是变量。


有时您必须帮助垃圾收集器,但这些通常(根据我的经验)与对象属性无关,因此不涉及删除>。它们仅在您创建函数实例时才会真正出现(如果您正在设置事件处理程序或计时器函数等,则这种情况很常见)。例如,考虑一下:

function foo() {
    var listOfThings = /* ...get a list of things... */;

    // ...do something with `listOfThings`...

    setInterval(function() {
       // ...do something that *doesn't* need `listOfThings`...
    }, 1000);
}

因为通过 setInterval 分配给计时器的匿名函数将在函数调用后继续存在,所以它会保留对该函数调用期间作用域内的所有内容的实时引用(无论是使用与否)。这会将 listOfThings 指向的事物列表保留在内存中。如果计时器函数不需要该列表,那就需要担心了。如果您知道该函数不需要 listOfThings 指向的列表,则可以通过将 undefinednull 或其他值分配给listOfThings 当你完成它时:

function foo() {

    var listOfThings = /* ...get a list of things... */;

    // ...do something with `listOfThings`...

    listOfThings = undefined; // Done with it           <== The new bit

    setInterval(function() {
       // ...do something that *doesn't* need `listOfThings`...
    }, 1000);
}

事件处理函数等也是如此。每当你创建一个函数时,它都会“关闭”(保留对其范围内的实时引用)任何内容被定义。因此,如果您不需要这些东西,您可以通过清除对它们的引用来确保它们不会保留在内存中。 (更多:闭包并不复杂

new and delete have nothing whatsoever to do with each other in JavaScript (despite their confusing similarity to completely different constructs in other languages). Don't worry about creating objects (new) without explicitly cleaning them up, that's the garbage collector's job.

new is for creating objects via constructor functions. delete, on the other hand, is for removing properties from objects. It has nothing to do with removing an object from memory, other than as a side effect (e.g., if the only outstanding reference to that object was from the property that you removed).

Example of correct use of delete:

var obj = {};
obj.foo = "bar"; // Now `obj` has a property called `foo`
delete obj.foo;  // Now it doesn't

Your getmyTime function is perfectly fine. The Date object will become eligible to be reclaimed immediately upon function return (whether it is reclaimed is completely down to the implementation). It does not cause a memory leak, except on a buggy implementation.

Your wasteSomeMemory2 similarly doesn't cause a memory leak, and in fact you can't call delete temp; — you can only delete properties, not vars.


There are times when you have to help the garbage collector out, but those usually don't (in my experience) have to do with object properties and so don't involve delete. They only really come up when you're creating function instances (which is fairly often, if you're setting up event handlers or timer functions, etc.). For instance, consider:

function foo() {
    var listOfThings = /* ...get a list of things... */;

    // ...do something with `listOfThings`...

    setInterval(function() {
       // ...do something that *doesn't* need `listOfThings`...
    }, 1000);
}

Because your anonymous function you've assigned to a timer via setInterval will survive the function call, it keeps a live reference to everything that was in-scope during that function call (whether it uses it or not). This keeps the list of things that listOfThings points to in memory. If the timer function doesn't need that list, that's a concern. You can release the list that listOfThings points to if you know that the function doesn't need it, by assigning undefined or null or whatever to listOfThings when you're done with it:

function foo() {

    var listOfThings = /* ...get a list of things... */;

    // ...do something with `listOfThings`...

    listOfThings = undefined; // Done with it           <== The new bit

    setInterval(function() {
       // ...do something that *doesn't* need `listOfThings`...
    }, 1000);
}

The same is true for event handler functions, etc. Whenever you create a function, it "closes over" (keeps a live reference to) anything in scope where it was defined. So if you don't need those things, you can ensure they're not kept in memory by clearing the references to them. (More: Closures are not complicated)

2024-10-22 03:36:46

简短的回答是否定的。

长的答案是我们希望上帝浏览垃圾收集器能捡起这个。

The short answer is no.

The long answer is we hope to god the browses garbage collector picks this up.

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