3 ms·
Garbage collection doesn't prevent memory leaks. Nothing stops you from filling up a Map and never emptying it
by mcluck 3y ago
Garbage collection doesn't prevent memory leaks. Nothing stops you from filling up a Map and never emptying it
- maxloh 3y agoMemory allocated for the map is supposed to be freed when the variable goes out of scope.
- tgv 3y agoNope. It's freed when there's nothing referencing it any longer, at the next garbage collect. Here are common pitfalls: https://auth0.com/blog/four-types-of-leaks-in-your-javascript-code-and-how-to-get-rid-of-them/ https://auth0.com/blog/four-types-of-leaks-in-your-javascrip...
- tom_ 3y agoIt's not a leak if the memory is still referenced! A garbage-collected system can never leak memory.
- bigyikes 3y agoYou’re being pedantic. “Memory leak” is colloquially used when you have data in memory which accumulates and isn’t actively being used by the system.
- deleted 3y ago[deleted]
- jmull 3y agoThe problem with that definition of "memory leak" is that it's not useful to programmers. A more useful definition is memory that remains allocated unintentionally.
- bmacho 3y agoHow is that more useful? According to your definition, languages have nothing to do with memory leaks, pro or contra.
- jmull 3y agoI think programmers mainly care about memory leaks because of the potential negative consequences of using up too much memory -- programs crash or fail, or services frequently restart, or you have to provision additional costly server resources to keep things running well. If your program continuously allocates memory that it doesn't free and doesn't serve a useful purpose, it doesn't really matter if it's, e.g., being referenced by some cache or just completely unreferenced -- it's still causing the same problem and has the same solution (that is, release the memory when you're done with it -- whether that's by calling "free" or clearing the reference, or whatever your memory management system has)