Live data from Hacker News

How JavaScript works: memory management and common memory leaks

blog.sessionstack.com

41–50 of 57 posts

Re: How JavaScript works: memory management and common memory leaks

#41

Who is this article written for? There's a whole section on "What is memory?". If you are optimizing to remove memory leaks I really hope you already know what memory is.

In my experience there are (I don't know how many) some programmers who, given how they were taught/learnt, can do productive work but generally don't know computing/programming in the abstract e.g. Memory at the machine level, or type systems.

Re: How JavaScript works: memory management and common memory leaks

#42
post #4

Wait, is #3 for real? If that's the case it seems like a huge oversight.

Yeah. I thought so too (and still think so). SO post: https://stackoverflow.com/questions/19798803/how-javascript-... Chrome bug report: http://crbug.com/315190 Meteor blog (linked in article): https://blog.meteor.com/an-interesting-kind-of-javascript-me... Live example (will crash due to memory leak): https://s3.amazonaws.com/chromebugs/memory.html --- The reason this exists in all JS engines is for performance; it'…

Yeah, there's a nice link in the comments on the chrome bug on how Lua does it with upvalues: https://bugs.chromium.org/p/chromium/issues/detail?id=315190...

Re: How JavaScript works: memory management and common memory leaks

#43
post #27
post #26

Earlier quoted context omitted.

Would it be better to have to explicitly declare what variables you want to import into the closure like PHP or C++ do? C# also captures everything by default and reference which has tripped me up quite a few times.

That might help but it seems like either an implementation or language spec fix is in order. There doesn't seem to be a reason for a function without free variables to turn into a closure at all, thus preventing the issue.

Yep.

Currently, the ECMAScript specification says nothing about GC.

And it seems every major JS engine has decided that this type of memory leak is okay.

So it's rather unlikely something will change.

Re: How JavaScript works: memory management and common memory leaks

#44
post #41

Who is this article written for? There's a whole section on "What is memory?". If you are optimizing to remove memory leaks I really hope you already know what memory is.

In my experience there are (I don't know how many) some programmers who, given how they were taught/learnt, can do productive work but generally don't know computing/programming in the abstract e.g. Memory at the machine level, or type systems.

can do productive work but generally don't know computing/programming in the abstract

e.g. Memory at the machine level,

I think it's the other way around --- their usual level of abstraction is too high to understand such things...

or type systems

...and slightly too low to understand others.

Re: How JavaScript works: memory management and common memory leaks

#46
post #2

having stuff do stuff for you is useful until it doesnt

I'm really glad that I use a garbage collected language. Unless I'm doing low level programming that requires controlling allocation and freeing, it's amazing. Yes, I still have to understand the basics of memory but I'm just very glad that most of the time, the basics are far more than enough.

Re: How JavaScript works: memory management and common memory leaks

#47
> As of 2012, all modern browsers ship a mark-and-sweep garbage-collector. All improvements made in the field of JavaScript garbage collection (generational/incremental/concurrent/parallel garbage collection) over the last years are implementation improvements of this algorithm (mark-and-sweep), but not improvements over the garbage collection algorithm itself, nor its goal of deciding whether an object is reachable or not.

WebKit uses a constraint-based garbage collector that does not rely on reachability alone. This is an improvement over the classical garbage collection algorithm.

Re: How JavaScript works: memory management and common memory leaks

#48

> As of 2012, all modern browsers ship a mark-and-sweep garbage-collector. All improvements made in the field of JavaScript garbage collection (generational/incremental/concurrent/parallel garbage collection) over the last years are implementation improvements of this algorithm (mark-and-sweep), but not improvements over the garbage collection algorithm itself, nor its goal of deciding whether an object is reachable…

For reference: https://webkit.org/blog/7122/introducing-riptide-webkits-ret...

Re: How JavaScript works: memory management and common memory leaks

#49
The main problem with JS being sluggish (in electron apps for example) is memory and especially the GC.

I can optimize CPU usage all I want, but only after I optimized for minimum allocations, the tiny, but noticeable lags now and then would disappear.

The average javascript-GC must be really simple/naive compared to seasoned workhorses like the JVM's various GCs.

There I can happily create millions of short-lived objects before getting problems in a single-user application.

Re: How JavaScript works: memory management and common memory leaks

#50
> To prevent these mistakes from happening, add 'use strict'; at the beginning of your JavaScript files. This enables a stricter mode of parsing JavaScript that prevents accidental global variables.

I don't think using strict will prevent accidental global variables, such as this.var in global scoped function calls. Strictness main goal is to prevent inadvertently misspelled variables from going unnoticed.

Post reply on HN