1

I've got a simple loop that creates and destroys (hopefully) empty objects: http://jsfiddle.net/TgWze/

function Test()
{
}

function generate()
{
    for(var i = 0; 1000 > i; ++i)
    {
        var view = new Test();

        delete view;
    }
}

The memory profile in Chrome/Safari shows memory-leak like behavior if I keep clicking the link: http://cl.ly/BnCV

Am I missing something?

5
  • Besides a possible memory leak, 'delete' is redundant since the reference will be out of scope after one loop anyway. Commented Nov 14, 2011 at 19:00
  • 3
    You can't actually use delete like that; it's only for deleting properties in an object. Commented Nov 14, 2011 at 19:01
  • 2
    @Dykam: Wrong. Javascript does not have block scope. Commented Nov 14, 2011 at 19:01
  • I stand corrected (too much perl lately), I meant to say that old view-variable would be out of scope in the next loop by redefinition. Commented Nov 14, 2011 at 19:15
  • 1
    Who keeps sending developers stuck with an manual-memory-management mindset to program in GC'd languages? Commented Nov 14, 2011 at 19:24

3 Answers 3

3

That looks like normal GC behavior. Once there are too many objects, the GC cleans them up.

It would only be a memory leak if the troughs after each peak (just after the GC runs) get successively higher, indicating that the GC didn't catch everything.

Sign up to request clarification or add additional context in comments.

Comments

3

It's managed memory. So it will collect the deleted object at some point when the garbage collector runs. You deleting the objects actually doesn't do anything. However since, view is never reference, it should be collected easily.

2 Comments

so, the key point here is that the object will be deleted next time Garbage Collector is run. This is conceptually different from C++ new/delete and that is the reason I see raise of the memory in profiler, b/c link is clicked too often and then, there is a drop in memory, which is due to garbage collector run. Thanks.
Exactly, although new does allocate some memory; delete does not deallocate it
0

That code does not leak.

To convince yourself you can take snapshots of the memory and compare before and after. Have a look at this guide that I wrote, for more details: http://www.vladalexandruionescu.com/2012/08/javascript-memory-leaks.html.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.