• Advertisement
Sign in to follow this  

Javascript Memory leak

This topic is 1400 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It's been a while since I coded javascript. this time I want to try making games with them because it seems that the technology is finally ready.

so I want to ask what are the things that causes memory leak in javascript

because my apps seems to be leaking all the time

 

I skim a few articles they all seems to be talking about circular dependencies and something to do with DOM

 

I tried simple code like

var d = 10;
 
setInterval ( function(){
d = d + 1;
}, 1 );

it seems to leak according to chrome dev tools 

I don't think this is a case of circular dependencies or anything to do with DOM

 

is this normal in javascript ?

Share this post


Link to post
Share on other sites
Advertisement

it seems to leak according to chrome dev tools 

 

Is this the whole code? How did you come to the conclusion that it leaks memory? That simple snippet should not leak any memory.

 

EDIT:

I just tried it myself. Even with a completely empty interval function the memory usage increases continually until chrome decides it's enough and frees a large block. This is completely normal behaviour and does not mean there is a memory leak. If you let the profiler run long enough you will see some kind of zig-zag pattern where memory usage increases up to a certain point and then suddenly drops. Older browsers used to have problems detecting circular references and thus never freeing some memory but in my experience this is mostly fixed now.

 

It is nevertheless a good practice to keep memory allocations low so the GC doesn't have to do as much work because it can slow down your application a lot.

Edited by Madhed

Share this post


Link to post
Share on other sites

this is a "bad" code, meaning chrome tool consdiers it a leak, though thanks to GC it will not leak for real. Realize you create a function every interval, and it seems like a global function, I do not see scope it would be local to. You should write



var d = 10;
var heavy= function(){
d = d + 1;
}

setInterval (heavy(), 1 );

Share this post


Link to post
Share on other sites

The code is not creating a function every interval. It is passed once as an argument, that is all.

Furthermore your code is not correct. heavy() returns undefined. So you are actually calling setInterval(undefined, 1);

 

 

Apart from that typo, both snippets are identical except for the fact that in OP's code the function is anonymous whereas in yours it's stored in the variable heavy.

Share this post


Link to post
Share on other sites

 

It is passed once as an argument, that is all.

it is not an argument passed once, what is written to setInterval is  javascript code that gets executed every time the interval passes. It is just this:

function(){d = d + 1;}

function(){d = d + 1;}

function(){d = d + 1;}

........

since it is not an r value, it gets also executed-entered

to my undefined typo, I gess it misses semicolon after the r-value , when you define var heavy=

 

Share this post


Link to post
Share on other sites

 

this is a "bad" code, meaning chrome tool consdiers it a leak, though thanks to GC it will not leak for real. Realize you create a function every interval, and it seems like a global function, I do not see scope it would be local to. You should write



var d = 10;
var heavy= function(){
d = d + 1;
}

setInterval (heavy(), 1 );

 

 

The code is not creating a function every interval. It is passed once as an argument, that is all.

Furthermore your code is not correct. heavy() returns undefined. So you are actually calling setInterval(undefined, 1);

 

 

Apart from that typo, both snippets are identical except for the fact that in OP's code the function is anonymous whereas in yours it's stored in the variable heavy.

 

Why does heavy() return undefined? Shouldn't return 11?

Share this post


Link to post
Share on other sites


what is written to setInterval is  javascript code that gets executed every time the interval passes

 

What is "written" to setInterval is a reference to the function.

 


since it is not an r value, it gets also executed-entered

 

I don't know why you are talking about r-values as this is javascript and not c++.

Share this post


Link to post
Share on other sites

@Alpha

 

In javascript functions without a return statement return the value undefined.

 

Duh. **slaps head**, **then reminds self it ain't lisp**, **slaps head again**

Share this post


Link to post
Share on other sites

 

The code is not creating a function every interval. It is passed once as an argument, that is all.

 

both snippets are identical except for the fact that in OP's code the function is anonymous whereas in yours it's stored in the variable heavy.

do you like confusing people that tries to get explained things, just becouse you are too frustrated to admit your proposition is incorrect? I will not downvote your posts like you did mine, let them be the "correct answers", I don't care

Share this post


Link to post
Share on other sites

gee, "heavy" is defined, "heavy()" is undefined, for it does not return anything.

 

 

I don't know why you are talking about r-values as this is javascript and not c++.

:(, r-value is a c++ termine? what the...

Share this post


Link to post
Share on other sites


do you like confusing people that tries to get explained things, just becouse you are too frustrated to admit your proposition is incorrect? I will not downvote your posts like you did mine, let them be the "correct answers", I don't care

 

I'm calm like a breeze, not frustrated at all.

Share this post


Link to post
Share on other sites

then do not get offtopic, and please explain me where I am wrong. You have already stated a whole pile of nonsense, while downvoting my answers. It is nice you are calm like a breeze though.

Share this post


Link to post
Share on other sites
// Creating a function in javascript

//1. directly named
function a() {};

//2. anonymously, assigned to variable
var b = function() {};

//3. via operator new (dont do this)
var c = new Function("arguments...", "body");

//4. anonymously, as a temporary variable (the temp is a reference to the function)
function() {};

OP creates a function when the script executes via method 4. The reference to the function is then passed to the internal setInterval function.

setInterval is internal to Chrome's V8 engine and repeatedly calls the function via reference. No additional functions are created.

 

If you can show me where my explanation is wrong, I welcome you to do so. If you continue attacking without reason I will assume that you are a troll.

Edited by Madhed

Share this post


Link to post
Share on other sites

setInterval(heavy(), 1);

 

If I'm not mistaken, heavy() will actually evaluate. And it will evaluate to undefined. So the heavy function needs to be:

function heavy()
{
      d = d + 1;
      return d;
}

evaluate to 11 (at least at the first pass).

 

Second the function being passed is only passed once, but is called by the setInterval function repeatedly.

 

Correct me if I'm wrong, but it should be setInterval(heavy, 1). Notice no () after heavy.

Share this post


Link to post
Share on other sites

/.../ and please explain me where I am wrong.

He already did:

What is "written" to setInterval is a reference to the function.


You have already stated a whole pile of nonsense, /.../

Except that his "pile of nonsense" is actually correct and your statements are not.

My guess is that you did not notice that the lambda function given to setInterval is not given as an string. This guess is also supported by the error you made in your example:

setInterval(heavy(), 1 ); // this is equal to: "setInterval(undefined, 1 )", given that heavy() returns "undefined".
instead of what you probably meant:
setInterval("heavy()", 1 );

Share this post


Link to post
Share on other sites

explaining me how javascript works is a nice way to tell me I have written something incorrect in my posts. In my post you can see a function without return value.  The fact  that OP is adding a return value to a function is not my fault. I have stated difference between heavy and heavy() , upon my typo in the first post.

 

The parameter of setInterval is an expression, a single command....setInterval(actions.action1,(new Primenum(22)).toInteger())....

 

the fact I have written about repeatedly calling this  is, becouse, though this is not setTimeout practice, the function is still allocated temporary and a reference to it does not exist, the reference existed as a temporary parameter only! The managing of this object is an outcome of gc. And plus, tell me how you plan to clearInterval if you do not know the reference of the function.

Share this post


Link to post
Share on other sites

@JohnnyCode:

 

There are two ways to use setInterval.

 

1. Pass function as a reference: setInterval(function() { doSomething(); }, 10);

 

2. Pass an expression as a string: setInterval("doSomething();", 10);

Share this post


Link to post
Share on other sites

yes, there are those to ways and I am apointing first one all the time, becouse you may not know that second one is dynamicly created function, just reinterpreted.

Share this post


Link to post
Share on other sites

wrong

 

1. Pass function as a reference: setInterval(function() { doSomething(); }, 10);

 

2. Pass an expression as a string: setInterval("doSomething();", 10);

 

those two are equal  , this is not

 

1. Pass function as a stored reference:

 

var f=function() { doSomething(); };

var hp=setInterval(f, 10);

 

 

...window.clearINterval(f);

 

or

 

clearINterval(hp);

Share this post


Link to post
Share on other sites

These are out of topic 

you keep arguing how to use setInterval which is a trivial thing for me ..

 

anyway, it just looks stupid that the garbage collector have that zigzag pattern at all

 

what memory does it allocate ? it uses one global variable and that's all

I didn't even declare a new variable.

 

This is strange indeed

Share this post


Link to post
Share on other sites

anyway, it just looks stupid that the garbage collector have that zigzag pattern at all
 
what memory does it allocate ? it uses one global variable and that's all
I didn't even declare a new variable.
 
This is strange indeed

1. garbage source.

While your example program itself does not visibly generate garbage - there still are plenty of garbage sources, a'la: virtual machine interpreting the javascript, jit compilation, whatever other internal structs it needs to implement the used or assumed to be used functionality of javascript.

Case in point of a possible source: calling a function needs a local scope/closure to be created and destroyed (technically - a good jit optimizer under specific conditions can prevent it from using generic GC for cleanup).

2. zigzag pattern.

Garbage collection is an overhead (ie. in a sense no useful work is done - GC is hardly the goal of any program tongue.png ). Hence GC implementations avoid actually doing it if they do not need to do it (ie. there is plenty of free memory) - allowing memory usage to steadily grow till it finally decides to do the work causing a sudden drop in memory usage => zigzag.

edit:

it seems to leak according to chrome dev tools

I have not used chrome dev tools, but i guess you are misinterpreting what it tells you.

I don't think this is a case of circular dependencies or anything to do with DOM

Yep. However, memory is not free till GC tells so => every unused crumb of memory piles up over time till GC gets rid of them all, all at once.

is this normal in javascript ?

Yes. It is common in all languages that heavily use mark-and-sweep style GC, not just javascript.

One might have a leak problem if memory usage grows over GC cycles (ie zigzag bottom line keeps growing). Edited by tanzanite7

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement