Re:Friendship ended with the garbage collector
by JoeAltmaier on Friday August 27, 2021 @11:39AM
(#28329215)
That's all fine for locals. How about members of objects? The object may be long-lived, the buffer it uses may be short-lived, and it can hang around unless you remember to null it.
Remembering to null references is not much easier than remembering to delete things. It's trading one issue for another.
And now there's two ways to remember to manage memory.
Re:Friendship ended with the garbage collector
by nine_k on Friday August 27, 2021 @01:10PM
(#28330369)
Easy: objects should not normally have members with different lifetimes, and ideally any mutable members at all.
There are exceptions to that, like queues and caches, but they are few and should be closely watched.
Most of the time (in my practice, like 99% of the time) you don't have to care about nulling references. The objects that need collection are created in local scopes of functions, and get detached and ready for GC right when the function returns. When you have many small functions, this happens often, so unused objects don't spend much time being referenced and ineligible for GC.
The problematic part is long-living mutable structures, which you hopefully have few, and know well where they are in your code.
Also, as an exercise, I suggest that you try to imagine a Lisp with explicit memory management.