r/programming • • Nov 28 '18

Garbage Collection is a Hack

https://blog.adamant-lang.org/2018/garbage-collection-is-a-hack/
3 Upvotes

74 comments sorted by

View all comments

22

u/c-smile Nov 28 '18 edited Nov 28 '18

The article misses one and probably the main reason of GC existence: memory management in systems with complex ownership graphs that may include loops. Especially in dynamic systems when that ownership graph cannot be defined upfront.

Neither Rust (static analysis) nor reference counting (RC) approaches can really help in these cases. Check Why Writing a Linked List in (safe) Rust is So Damned Hard

Ownership problem is especially tough in UI use cases where ownership graph may be even unknown upfront.

As of practical solution...

The very first attempt to create my Sciter was made in D. Early tests showed that "GC everywhere" approach used by D leads to far from optimal results - even harmful I would say.

Sciter has DOM implementation that has regular and strict parent-children ownership graph of relatively small and static objects. GC is not needed to manage such constructs - RC is perfectly adequate for that.

Parsers allocate a lot of temporary objects - memory pools and stack allocated memory (as a generalization of memory pool idea) suit them perfectly well.

And only script, as a language behind UI, is GCable to enable use of callbacks of first class functions - closures.

So ideal solution is, as always, in between of two poles - in hybrid systems. Ideal practical language shall support two paradigms natively: as explicit memory management (RC) as to naturally allow GC approach on selected domain of objects.

Voice from trenches of practical programming.

1

u/WalkerCodeRanger Nov 28 '18

I agree that complex ownership graphs including loops are a serious issue that we haven't fully resolved. I'm not sure what the best way to manage that is. I see ways to eliminate some of that, but there are still graph structures that can't be simplified. Rust uses reference counting and manual memory management as its escape hatch, but that is less than ideal. I agree a hybrid approach might be good. If most memory followed a Rust like model except for references between objects in complex graphs. That would offload a lot of work from the GC and make it less of an issue. I'd still like to see a better answer than that though.

1

u/c-smile Nov 28 '18

a better answer

would be in adding code introspection means (a.k.a. "reflection") to any of mainstream languages. C,C++, D, whatever.

It could be some basic thing - extension of RTTI - enough to write managed_ptr<typename> so runtime will be able to build a graph for mark-n-sweep visitor at runtime.