Sign in to follow this  
Darkbouncer4689

Javascript - Memory allocation

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Concentrate    181
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.

Share this post


Link to post
Share on other sites
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 =)

Share this post


Link to post
Share on other sites
Gamer Gamester    140
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).

Share this post


Link to post
Share on other sites
wqking    761
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this