Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

object - `new` without `delete` on same variable in Javascript

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...