News for hackers. Stuff that matters.
Re:Friendship ended with the garbage collector by JoeAltmaier on Friday August 27, 2021 @10:44AM (#28328474)
I'm amused when I see all the hoops folks jump thru to solve 'memory reference bugs'. Garbage collectors 'leak' if you forget to null a reference in scope after you're done with it. Those null pointers can fault if used afterward.

Its as much work to use a garbage collector efficiently as it is to allocate and delete your own memory. And with about the same bugs. It's been problematic from the start.

Not a fan of automatic systems to 'manage' memory. Overhead for automatic systems create lag, latency and bloat. I'm old-fashioned, and create code that has strict ownership of allocated memory by subsystem. If a module that allocates also frees, leaks become negligible. If references are not shared but accessed formally, nulls become returned errors etc.

Discipline is hard, but leaky buggy garbage-collecting behemoths are hard to live with too.

Re:Friendship ended with the garbage collector by messe on Friday August 27, 2021 @11:32AM (#28329127)
You might be interested in Zig[1]. Allocation is manual. Standard library containers and functions require an allocator to be passed to them, so nothing allocates on the heap unless it's explicitly told to. Cleanup can be done at end of scope via defer / errdefer.

Seamless interop with C, even when cross compiling which Zig handles natively: the standard install of Zig comes with a "zig cc" wrapper around clang that allows you to cross compile[2][3] C and zig code for any number of target architectures (it includes C standard library headers and code for them).

Zig is the only language I've come across which functions perfectly as a better C.

[1]: https://ziglang.org

[2]: https://actually.fyi/posts/zig-makes-rust-cross-compilation-...

[3]: https://andrewkelley.me/post/zig-cc-powerful-drop-in-replace...

Re:Friendship ended with the garbage collector by nine_k on Friday August 27, 2021 @11:32AM (#28329138)
I think you slightly overstate the problem.

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.

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.

Re:Friendship ended with the garbage collector by echelon on Friday August 27, 2021 @11:59AM (#28329452)
I'm laughing, because Rust solves all of this and is a simple and easy language to learn. No GC, ref counts when you want or need them, and manual memory management that is a breeze and proven at compile time. I hate even saying this because I get the feeling it scares away newbies that think these concepts are hard.

I already write web services in Rust. It's no more difficult than Java or Python.

I can't wait for the ecosystem to heat up more, because it'll make it compelling to switch to Rust at work.

Re:Friendship ended with the garbage collector by zozbot234 on Friday August 27, 2021 @12:11PM (#28329569)
Rust is certainly easier to teach and learn than some other languages (think C++) but learning it effectively still requires quite a bit of discipline. It's not really made for the bug prone cowboy-coding style that seems to be the norm in more 'dynamic' languages (Python, JavaScript, Ruby etc.) and to a lesser extent in more mainstream, 'enterprise' friendly languages like Go, Java/Scala/Kotlin, C#/VB.NET, Erlang etc.
Re:Friendship ended with the garbage collector by echelon on Friday August 27, 2021 @12:19PM (#28329669)
Fair point. If you want to hack something together and don't care too much for the details, Rust will slow you down.

But if you care enough to be writing tests or put lots of thought into API and schema, then maybe you're in the disciplined camp that would get a ton of benefit from switching.

If you're using C++, you should switch for any greenfield project that doesn't need to use legacy libraries. (Writing wrappers for C libraries isn't hard at all.)

Re:Friendship ended with the garbage collector by pjmlp on Saturday August 28, 2021 @10:06AM (#28338171)
What about using C++ in LLVM to support rustc?
Re:Friendship ended with the garbage collector by traverseda on Friday August 27, 2021 @12:00PM (#28329464)
>Its as much work to use a garbage collector efficiently as it is to allocate and delete your own memory.

Typically I just use weak references or weak reference sets/dicts/whatever. That's python for you though.

I presume you mean it's just as hard in some specific language, not just as hard in general.