can't release memory

T

tal.shemesh

Hi,
Recently i started to work with javascript and notice that i have
a lot of memory leaks.
I built simple example which creates 100,000 strings and then i
delete them and
the memory is still there :(

any idea what should i do?


code:
var X = [];
var y;
debugger;
for (y = 0 ; y < 100000 ; y++){
X[y] = "fdsfsddfsdfsdfsdfsdfdsfdsfsdfsdf";
}
debugger;
X = null;
delete X;
 
A

Amrit Ranjan

Hi,
Recently i started to work with javascript and notice that i have
a lot of memory leaks.
I built simple example which creates 100,000 strings and then i
delete them and
the memory is still there :(

any idea what should i do?

code:
var X = [];
var y;
debugger;
for (y = 0 ; y < 100000 ; y++){
X[y] = "fdsfsddfsdfsdfsdfsdfdsfdsfsdfsdf";
}
debugger;
X = null;
delete X;

delete key word is not applicable for those variable which are
declared with var keyword. Please refer following site.
http://developer.mozilla.org/en/doc...e:Operators:Special_Operators:delete_Operator
If you really want to clear the memory use Array.splice method
example
X.splice(0, X.length);
this method will clear the memory of the X, but X is still an array.
Now make X = null.
Do not assign X = [] again. It will just change the reference
pointer. any way as because var is used and the execution sample is in
function, it will clear the memory when scope goes out.

Thanks,
Amrit Ranjan
 
T

Thomas 'PointedEars' Lahn

Amrit said:
delete key word is not applicable for those variable which are
declared with var keyword. Please refer following site.
http://developer.mozilla.org/en/doc...e:Operators:Special_Operators:delete_Operator

`delete' is applicable in that case, but it does not change anything.
If you really want to clear the memory use Array.splice method
example
X.splice(0, X.length);
this method will clear the memory of the X,

Certainly it won't. The memory assigned to the values of the elements of
the array *may* be freed but it could also be that the corresponding objects
are merely marked for garbage collection.
but X is still an array.

Which is why the memory required for storing that object is only reduced, if
that.
Now make X = null.

That assignedment could have been performed in the first place and it would
have made little difference. That is, with that assignment X is marked as
being ready for garbage collection, and the objects its properties refer to
as well, provided there are no further references to either object.
Do not assign X = [] again. It will just change the reference
pointer.

There is no significant difference in assigning [] and calling
X.splice(0, X.length). See ECMAScript Ed. 3, section 15.4.4.12.
any way as because var is used and the execution sample is in
function, it will clear the memory when scope goes out.

It *may* clear the memory then.

I really wonder where your "wisdom" comes from.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Amrit said:
Now make X = null.

That [assignment] could have been performed in the first place and it
would have made little difference. That is, with that assignment X is
marked as being ready for garbage collection, [...]

Correction: the object X refers to is marked so as well as
[...] the objects its properties refer to [...], provided there are no
further references to either object.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,373
Latest member
Desiree036

Latest Threads

Top