Advertisement Jump to content
Sign in to follow this  
rmxhaha

Javascript Memory leak

This topic is 1761 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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!