• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
rmxhaha

Javascript Memory leak

26 posts in this topic

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 ?

2

Share this post


Link to post
Share on other sites

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
2

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

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.

0

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=

 

-3

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?

0

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

0

Share this post


Link to post
Share on other sites

@Alpha

 

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

0

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

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.

 

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

-3

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

0

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.

0

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.

-4

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
2

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.

0

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

Share this post


Link to post
Share on other sites


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

 

Ah, now it makes sense.

0

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.

-4

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

1

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.

-2

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

-4

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

0

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
2

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  
Followers 0