News for hackers. Stuff that matters.
Re:Reference count, don't garbage collect by kaba0 on Friday July 29, 2022 @01:38PM (#32279770)
One great example would be a C++ program that runs fast, and then just spends 10s of seconds doing “nothing” while it deallocates shared pointers’ huge object graphs at the end. They really are two sides of the same coin, with tracing GCs being actually correct (you need cycle detection for a correct RC implementation), and having much better throughput. It’s not an accident that runtimes with thousands dev hours are polishing their GC solutions instead of using a much more trivial RC.
Re:Reference count, don't garbage collect by hinkley on Friday July 29, 2022 @06:11PM (#32282354)
I don't know what the current state of the art is, but at one point the answer to GC in a realtime environment was to amortize free() across malloc(). Each allocation would clear up to 10 elements from queue of free-able memory locations. That gives a reasonably tight upper bound on worst case alloc time, and most workflows converge on a garbage-free heap. Big malloc after small free might still blow your deadlines, but big allocations after bootstrapping are generally frowned upon in realtime applications, so that's as much a social problem as a technical one.
Re:Reference count, don't garbage collect by pclmulqdq on Friday July 29, 2022 @08:01PM (#32283039)
This is normal today inside malloc implementations.
Re:Reference count, don't garbage collect by hinkley on Saturday July 30, 2022 @11:32AM (#32287575)
I think that’s been normal in malloc for some time. I maintain that we should all thank Java, and the hype train around it, for forcing operating systems to implement more robust threading support. Everyone wanted to be able to say they did Java well, and nearly every OS had capital ‘I’ Issues, including Solaris. Boy I bet those closed room arguments were epic.

Where GCed languages pulled ahead for a while, particularly Java, was that they had mature concurrent memory allocation before most OSes did, so many people found on early two and four core hardware that malloc started to show up as a bottleneck.

Re:Reference count, don't garbage collect by pklausler on Friday July 29, 2022 @07:24PM (#32282826)
It's not marketed as a GC, but exit(2) is fast and effective when used as one.
Re:Reference count, don't garbage collect by game-of-throws on Friday July 29, 2022 @07:59PM (#32283032)
I wouldn't call it a GC. It's more like an arena allocator, with the arenas managed by the OS :)
Re:Reference count, don't garbage collect by saagarjha on Friday July 29, 2022 @07:51PM (#32282987)
Now you've pushed the job of cleaning up page tables to the OS.
Re:Reference count, don't garbage collect by chrisseaton on Friday July 29, 2022 @07:55PM (#32283003)
But that’s extremely easy - you go through a linear list once and free them all. You don’t even need to bring the contents from main memory to do it.

(Exception is shared pages, not used for heap memory often.)

Re:Reference count, don't garbage collect by saagarjha on Friday July 29, 2022 @08:11PM (#32283097)
It's definitely easier, because you don't have interior pointers. But it's still work :)
Re:Reference count, don't garbage collect by int_19h on Saturday July 30, 2022 @02:55AM (#32284893)
It's optimized for that, so just let it do its job.

https://devblogs.microsoft.com/oldnewthing/20120105-00/?p=86...

Re:Reference count, don't garbage collect by jeeeb on Saturday July 30, 2022 @12:05AM (#32284192)
The OS would have that job anyway.
Re:Reference count, don't garbage collect by im3w1l on Friday July 29, 2022 @07:09PM (#32282722)
Fun fact: if you dont do anything important in the destructors you can avoid that delay by intentionally leaking the memory. The os will clean it up when the program exits and it does a better job since it frees the pages rather than looking at your objects one by one.
Re:Reference count, don't garbage collect by kaba0 on Friday July 29, 2022 @07:25PM (#32282831)
Most GCs do exactly that -- they only "work" when absolutely necessary, and their heuristics says that they are getting behind the created garbage. If the program exits shortly after it will just leak the memory.

The problem with that in the case of C++ is that you likely only want to leak things used in the end from the main thread, but not "recursively" - the distinction is hard to do.

Re:Reference count, don't garbage collect by wizofaus on Saturday July 30, 2022 @05:20AM (#32285420)
It actually wouldn't surprise me that's the main reason GC often outperforms ARC in real- life software - simply because it's only necessary to actually bother doing GC once memory usage is high and there's a known need to allocate further memory. But couldn't ARC do the same thing in principle - i.e. only bother incrementing/decrementing reference counts when there's likely to be a need to reclaim memory? Even an infinitely running service can just accept it will increase its memory footprint up to a certain threshold then only bother with memory management at that point (I actually suspect SQL server works exactly like this!). For most short-running tools, no MM is required - the OS handles everything.
Re:Reference count, don't garbage collect by kaba0 on Saturday July 30, 2022 @06:45AM (#32285741)
As mentioned elsewhere in the thread, the actual reclamation can be done asynchronously, but the most problematic part, namely decrementing a counter atomically is the most problematic part and it can’t just be done later willy-nilly.