Javascript - Memory allocation

Started by
4 comments, last by Darkbouncer4689 12 years, 8 months ago
Hey all,

I'm currently stumped as to how javascript handles memory. For a while I assumed it was similar to python or java and just "handled things for you", but as I have a memory leak and have been looking into the delete keyword I am figuring that is not the case.

Through testing it seems like javascript will sometimes handle things for you and other times it won't. I can't figure out how it works.

If I do something like var list = new Array(10); and then populate it with values
and then call delete list

It will return false. Also it seems that without delete it still frees up the memory and will not cause a leak.

Also something like var time = new Date().getTime()

does not need to be deleted.

Any clarifications are appreciated.
Advertisement
I'm guessing that in javascript, it has garbage collection based on reference counting. Usually memory leaks in these systems in javascript are caused by circular references, in which the system fails to identify. The delete keyword in javascript is not really meant to free and object, but rather something like remove a property from an object. Anyways, I'm not detailed in javascript so I can't provide you with a complete answer, but maybe my though is worth something.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
Thank you for your post. You may be right. I've narrowed down my memory leak to a function that builds my skeleton for animation. The function uses a stack to go through a tree hierarchy and calls the function recursively. I suspect your post may be dead on. I'll look into it more. Thanks =)
Yes, in JavaScript the 'delete' operator is solely for deleting object properties and array elements. It's the only way to fully delete a property on an object (setting the property to 'undefined' might appear to be equivalent, but in that case the 'in' operator will still say that the property exists on the object).

JavaScript is garbage collected, so to free memory you simply have to eliminate all references to it (by setting any referring variables to 'null' or 'undefined' or anything else). The 'delete' operator will work too if the memory in question is an object property (or array element), and if that property is the sole reference to that memory.

I think modern JavaScript interpreters are getting better with handling circular references (but I may be mistaken!). Also pay attention to closures and active event listeners (it's a good idea to remove these when they're no longer needed).
As far as I understand,

1, "delete" is only to remove elements from object/array. Setting elements to null will cause the elements are still there with null values. (I think it's implementation dependency though)
2, You don't need and also you can't free any resource. The GC will do it for you.

Anyway, ECMA delete != C++ delete

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

It seems like the memory leak is coming from Webgl. It's been really hard to track the cause and may be a bug out of my control.

This topic is closed to new replies.

Advertisement