Hacker News new | ask | show | jobs
by milesrout 479 days ago
Memory leaks are not possible in garbage collected languages unless you retain references to data, but by definition that isn't a memory leak, that is exactly the behaviour that you want.

Memory leaks are situations where memory is unrecovered despite there being no path to it from any active thread.

2 comments

This is the same definition game you’re accusing Rust of making. Sometimes, you retain references you do not want, and therefore, leak. It’s something that comes down to programmer intent.

Talking about leaks this way is absolutely normal. Take https://stackoverflow.com/questions/6470651/how-can-i-create... for tons of examples.

So for example, if I do:

    static std::weak_ptr<std::array<uint64_t, 125000000>> weak;

    std::shared_ptr<std::array<uint64_t, 125000000>> strong = std::make_shared({0});
    weak = std::weak_ptr(strong);
That retains 1GiB of memory allocated without any ownership path due to implementation details of std::shared_ptr. Is that a memory leak? There’s no active thread that has a path and yet all of the memory is tracked - if you destroy the weak_ptr thee 1GiB of memory gets reclaimed.
std shared_ptr uses reference counting, not automatic memory management (gc).
Reference counting is a form of GC / automatic memory management [1] but it’s ok, it’s a common mistake to make. What’s less ok is this absolute intransigence in persisting to believe that memory leaks aren’t possible in tracing GCs but only when playing the same definitional games you accuse Rust of doing by limiting the types of things you count as leaks. For example, if I implement a cache as Map<String, Object>, that’s a memory leak if you define memory leaks as retaining memory longer than you’d actually need if the goal is to have just a single instance of a value for key live (because it’s not using a weak reference) or forgetting to delete/evict from the cache. Bad software design can result in memory leaks and defining it as not a memory leak because a live reference to an object exists somewhere is just playing the definitions game [2]

[1] https://en.m.wikipedia.org/wiki/Garbage_collection_(computer...

[2] https://stackoverflow.com/questions/4987357/can-there-be-mem...

You have misunderstood both the concept of a memory lwak and the concept of automatic memory management. Good job!

No, reference counting is not garbage collection. I am fully aware of the ridiculous claim that it is, promoted by people like you. I fundamentally disagree. It has none of the same properties and doesn't work anything like GC.

https://dl.acm.org/doi/10.1145/1028976.1028982

It’s not a “ridiculous claim”, but maybe you think cycle collectors don’t count?

Multiple very talented and very knowledgeable people have tried to help you understand and these are people with firsthand knowledge of the discussion at hand (I’m not counting myself because Steve and the other know language design and Rust better than I do). You insist on doubling down on your position instead of considering the possibility you’re wrong. Not much more I can do here. You can only lead a horse to water.
I consider whether I am wrong often. It happens to be that I am not. It is quite haughty and rude of you to assume that I haven't considered it here just because I disagree with you.

There isn't much more you can do here because you are completely wrong. Instead of facing reality (that Rust, useful as it may be, only prevents a narrow class of correctness issues of varying importance) you double down on its marketing spin that all the things it fixes just happen to be all the important safety-related ones.

Just step back and actually think. I implore you.