Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Javascript Memory leak


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
26 replies to this topic

#1 rmxhaha   Members   -  Reputation: 198

Like
2Likes
Like

Posted 25 March 2014 - 10:02 AM

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 ?



Sponsor:

#2 Madhed   Crossbones+   -  Reputation: 3140

Like
2Likes
Like

Posted 25 March 2014 - 11:01 AM


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, 25 March 2014 - 11:18 AM.


#3 JohnnyCode   Members   -  Reputation: 339

Like
-4Likes
Like

Posted 25 March 2014 - 12:07 PM

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 );


#4 Madhed   Crossbones+   -  Reputation: 3140

Like
0Likes
Like

Posted 25 March 2014 - 12:23 PM

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.



#5 JohnnyCode   Members   -  Reputation: 339

Like
-3Likes
Like

Posted 25 March 2014 - 12:35 PM

 

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=

 



#6 Alpha_ProgDes   Crossbones+   -  Reputation: 4692

Like
0Likes
Like

Posted 25 March 2014 - 12:40 PM

 

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?


Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#7 Madhed   Crossbones+   -  Reputation: 3140

Like
0Likes
Like

Posted 25 March 2014 - 12:40 PM


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++.



#8 Madhed   Crossbones+   -  Reputation: 3140

Like
0Likes
Like

Posted 25 March 2014 - 12:42 PM

@Alpha

 

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



#9 Alpha_ProgDes   Crossbones+   -  Reputation: 4692

Like
1Likes
Like

Posted 25 March 2014 - 12:43 PM

@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**


Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#10 JohnnyCode   Members   -  Reputation: 339

Like
-3Likes
Like

Posted 25 March 2014 - 12:46 PM

 

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



#11 JohnnyCode   Members   -  Reputation: 339

Like
0Likes
Like

Posted 25 March 2014 - 12:49 PM

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...



#12 Madhed   Crossbones+   -  Reputation: 3140

Like
0Likes
Like

Posted 25 March 2014 - 12:52 PM


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.



#13 JohnnyCode   Members   -  Reputation: 339

Like
-4Likes
Like

Posted 25 March 2014 - 12:55 PM

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.



#14 Madhed   Crossbones+   -  Reputation: 3140

Like
2Likes
Like

Posted 25 March 2014 - 01:11 PM

// 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, 25 March 2014 - 01:12 PM.


#15 Alpha_ProgDes   Crossbones+   -  Reputation: 4692

Like
0Likes
Like

Posted 25 March 2014 - 01:15 PM

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.


Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#16 tanzanite7   Members   -  Reputation: 1379

Like
1Likes
Like

Posted 25 March 2014 - 01:16 PM

/.../ 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 );


#17 Madhed   Crossbones+   -  Reputation: 3140

Like
0Likes
Like

Posted 25 March 2014 - 03:12 PM


instead of what you probably meant:
setInterval("heavy()", 1 );

 

Ah, now it makes sense.



#18 JohnnyCode   Members   -  Reputation: 339

Like
-4Likes
Like

Posted 25 March 2014 - 05:13 PM

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.



#19 Madhed   Crossbones+   -  Reputation: 3140

Like
1Likes
Like

Posted 25 March 2014 - 05:26 PM

@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);



#20 JohnnyCode   Members   -  Reputation: 339

Like
-2Likes
Like

Posted 25 March 2014 - 05:32 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS