Live data from Hacker News

How JavaScript works: memory management and common memory leaks

blog.sessionstack.com

1–10 of 57 posts

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

#3
post #2

having stuff do stuff for you is useful until it doesnt

I agree. I strongly dislike "magic" in programming. I prefer to call it "denial" because you need to know about the complexities anyway, and "magic" often means sweeping them all under the rug.

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

#5
post #4

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

Yeah, of the four things listed, I think it's the one that would trip me up the most. I think the safety net here would be dead code elimination: `unused()` should be detected as never called and removed during compilation/transpilation so that this wouldn't be an issue.

I am sort of surprised that `unused` is not picked up by the garbage collector in the first place though. Since JS functions are objects, shouldn't it detect an object that's not referenced during the mark and sweep?

In general, I really hate having to debug memory leaks in JS or Python. The interpreter for both will randomly allocate additional memory as it runs, so using tools like Valgrind is next to impossible. The only reliable method I've found is to pepper my code with logging statements that show what the current memory usage is, run the code like 1000-10,000 times, and see the points between which the memory usage goes up without coming down on a consistent basis. Python's built in `gc` module seems nearly useless for determining what's actually stuck in memory, and having a billion libraries that can have their own memory leaks is also not fun. These are the times I miss C: when you leak memory in C you know it because it becomes painful fast and it's usually easy to find, if your code is sane.

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

#6
post #2

having stuff do stuff for you is useful until it doesnt

I agree. I strongly dislike "magic" in programming. I prefer to call it "denial" because you need to know about the complexities anyway, and "magic" often means sweeping them all under the rug.

Exactly, you need to de-mystify the garbage collector so that it does what you want it to do.

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

#8

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.

if you program you have some concept of what memory is. I found the review of basic terms helpful in understanding the common JS leaks. YMMV
Post reply on HN