Hacker News new | past | comments | ask | show | jobs | submit login

> The difference is that if you get it wrong in a managed language, you get leaks or stale objects or other logic bugs.

Can you provide concrete examples of this? I've literally never had a bug due to the nature of a memory-managed language.




Once upon a time (at least through IE7) Internet Explorer had separate memory managers for javascript and the DOM. If there was a cycle between a JS object and a DOM object (a DOM node is assigned as a property of an object, and another property was assigned as an event handler to the DOM node) then IE couldn't reclaim the memory.

Developers of anything resembling complex scripts (for the time) had to manually break these cycles by setting to null the attributes of the DOM node that had references to any JS objects.

Douglas Crockford has a little writeup here[0] with a heavy-handed solution, but it was better than doing it by hand if you were worried another developer would come along and add something and forget to remove it.

Other memory managed languages also have to deal with the occasional sharp corners. Most of the time, this can be avoided by knowing to clean up resources properly, but some are easier to fall for than others.

Oracle has a write up on hunting Java memory leaks [1] Microsoft has a similar, but less detailed article here[2]

Of course, sometimes a "leak" is really a feature. One notorious example is variable shadowing in the bad old days of JS prior to the advent of strict mode. I forget the name of the company, but someone's launch was ruined because a variable referencing a shopping cart wasn't declared with `var` and was treated as a global variable, causing concurrent viewers to accidentally get other user's shopping cart data as node runs in a single main thread, and concurrency was handled only by node's event loop.

[0] https://www.crockford.com/javascript/memory/leak.html

[1] https://docs.oracle.com/en/java/javase/17/troubleshoot/troub...

[2] https://learn.microsoft.com/en-us/dotnet/core/diagnostics/de...


My question was about the nature of a memory-managed language causing "leaks or stale objects or other logic bugs". This issue is not that - this is due to a buggy implementation causing memory leaks.

To be more precise: this is a bug, that was fixable, in the runtime, not in user applications that would run on top of it.

Assume a well-designed memory-safe language and implementation. What kinds of memory hazards are there?


> separate memory managers

Notwithstanding the rest of your comment, this doesn't seem like a good example of the problem, since most GCs have a complete view of their memory.


CMEs in Java are a constant thorn to many Java programmers as a lifetime violation bug. Hell even NPEs are too for that matter, lol.


You can certainly get memory abandonment, which is like a leak but for memory that's still referenced and is just never going to be used again.


I will note that in GC literature at least, that is still considered a leak.

In an ideal world, we could have a GC that reclaimed all unused memory, but that turns out to be impossible because of the halting problem. So, we settle for GCs that reclaim only unreachable memory, which is a strict subset of unused memory. Unused reachable memory is a leak.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: