Jump to content

  • Log In with Google      Sign In   
  • Create Account

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

#21 JohnnyCode   Members   -  Reputation: 294

Like
-4Likes
Like

Posted 25 March 2014 - 05:37 PM

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



Sponsor:

#22 rmxhaha   Members   -  Reputation: 195

Like
0Likes
Like

Posted 26 March 2014 - 12:42 AM

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



#23 tanzanite7   Members   -  Reputation: 1378

Like
2Likes
Like

Posted 26 March 2014 - 03:23 AM

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, 26 March 2014 - 03:40 AM.


#24 TheComet   Crossbones+   -  Reputation: 1644

Like
4Likes
Like

Posted 26 March 2014 - 03:44 AM

For the record, the correct term is "sawtooth" and not "zig zag pattern"

(sorry for being a jackass)


YOUR_OPINION >/dev/null

#25 tanzanite7   Members   -  Reputation: 1378

Like
2Likes
Like

Posted 26 March 2014 - 04:05 AM

biggrin.png, true that.

duty_calls.png

(sorry for being a jackass - i am just easily amused)

#26 rmxhaha   Members   -  Reputation: 195

Like
0Likes
Like

Posted 26 March 2014 - 07:55 AM

Ok thanks for all the reply,,

I get it now,

 

I never pay attention to GC mechanism before 

since in python ( the other intepreted language I know ever used ) the memory doesn't seem to have this sawtooth

I directly assume that it's a leak when I saw that

 

so "sawtooth" is normal in javascript

 

btw, how do you announce this thread as solved ?



#27 JohnnyCode   Members   -  Reputation: 294

Like
-1Likes
Like

Posted 26 March 2014 - 09:31 AM

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

 

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

 

 

again, this generates a total leak! For the parameter is stored smartly in object that setInterval returns, but you do not store this returned object.  and the reference to parameter that gets allocated in expresion gets uncought. You can afford not storing the retuned object only if you pass an allocated function reference! You are all here trying to get experts in javascript finding out I am writing correct informations.

 

again to make it clear , though I am getting sick of you

 

var tm=setInterval(function(){alert('');},100) ;// does not generate leak

 

setInterval(function(){alert('');},100) ; // generates total leak

 

var func=function(){alert('');};

setInterval(func,100) ;  // does not generate leak

 

I have also said that those given 2 ways you described are interpreted by JIT compiler as totaly the same, what is totaly true information, but it got willingly downvoted automaticly. This very invalidates the potential of this portal, that any dumb 15 year old expert can just do what his low selfesteem needs to.






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