Live data from Hacker News

Garbage Collection is Wrong

lb-stuff.com

31–40 of 111 posts

Re: Garbage Collection is Wrong

#31
post #26
post #23

Earlier quoted context omitted.

RAII is great, and should be used where appropriate. I'm not sure what it has to do with reference counting, though. Reference counting is just a particularly slow and unreliable form of garbage collection; I don't see why you would ever prefer it to a proper garbage collector.

Classic RAII in C++ is a limited form of ref-counting (only one ref). I have to disagree with your second statement, ref-counting is extremely fast. If you consider it a GC then it's the fastest GC. It's also deterministic and does not pause.

> If you consider it a GC then it's the fastest GC. It's also deterministic and does not pause.

No, it's not, not unless you use a lot of cleverness. "We find that an existing modern implementation of reference counting has an average 30% overhead compared to tracing…" (They did perform a lot of optimizations to get it up to speed with tracing garbage collection... however, these are far beyond what shared_ptr does.)

http://users.cecs.anu.edu.au/~steveb/downloads/pdf/rc-ismm-2...

Re: Garbage Collection is Wrong

#32
post #21

Earlier quoted context omitted.

"Yes, in some scenarios you can associate the life time of a resource with the life time of a storage location" In what cases is this not possible? I model the resource as an object and that object has a memory location. (Yes, in a non-deterministic GC, you can't do this for expensive resources, but that's a problem of GCs, not an in-principle problem). EDIT: I am guessing the parent actually meant "associate the lif…

For example, two lists both contain a reference to the same object. Neither one is a sole owner, and you can't safely destroy the object until both lists are destroyed.

Then the lists should only have a weak_ptr to the object.

Something is handling both list [1], that something could own the object, or maybe something external to that. Giving ownership of the object to both list is a design error [2].

[1] e.g. if the two list are an implementation detail of a data structure, the data-structure itself could own the objects in the lists.

[2] Do non-deterministic garbage collectors that handle cycles allow you to have a resource with multiple owners? Yes. Should you do it? No, god, please don't.

Re: Garbage Collection is Wrong

#34
post #25
post #7

Yes, in some scenarios you can associate the lifetime of a resource with the lifetime of a storage location, but this simply does not work in all cases, probably only in a small fraction of all cases. And then? How do you handle resources that have no single obvious owner? How do you determine if the resource is still in use when you are done with it in one place? You implement some kind of reference counting? You ke…

Examples of a situation where you don't want a resource associated with a variable/storage location? Global variables exist and can be used to hold resources. If you have a resource that's not associated with any single function context you can move it into the global variable.

See mikeash's comment for a very simple situation. I mostly develop business applications with customers, products, orders, contracts, addresses and hundreds of other things connected and entangled in sometimes insane ways as dictated by the business. And at any point the user may just open another form viewing the orders of a different customer linked to the same products as in five other forms. Managing such complex object graphs by hand would be a nightmare. And this is just the model of the business, we did not even start to think about database connections, client connections, UI components and what not.

Re: Garbage Collection is Wrong

#35
post #32
post #21

Earlier quoted context omitted.

For example, two lists both contain a reference to the same object. Neither one is a sole owner, and you can't safely destroy the object until both lists are destroyed.

Then the lists should only have a weak_ptr to the object. Something is handling both list [1], that something could own the object, or maybe something external to that. Giving ownership of the object to both list is a design error [2]. [1] e.g. if the two list are an implementation detail of a data structure, the data-structure itself could own the objects in the lists. [2] Do non-deterministic garbage collectors tha…

It's only a design error when you don't have a GC.

Imagine you have an arbitrary long lived connected cyclic graph that can be incrementally updated from multiple short lived worker threads. With a GC, this is a no brainer: just put the objects in the graph. No workarounds, no extra tracking.

Without a GC, on removing a node, you have to walk to essentially do a mark/sweep of the graph to find dead nodes that were connected through the node you removed.

Re: Garbage Collection is Wrong

#36
post #25
post #7

Yes, in some scenarios you can associate the lifetime of a resource with the lifetime of a storage location, but this simply does not work in all cases, probably only in a small fraction of all cases. And then? How do you handle resources that have no single obvious owner? How do you determine if the resource is still in use when you are done with it in one place? You implement some kind of reference counting? You ke…

Examples of a situation where you don't want a resource associated with a variable/storage location? Global variables exist and can be used to hold resources. If you have a resource that's not associated with any single function context you can move it into the global variable.

> Global variables exist

And are generally considered bad practice.

Re: Garbage Collection is Wrong

#38
Worth a mention: In Objective-C with ARC, the compiler treats every pointer to an Obj-C object as a std::shared_ptr or std::unique_ptr (static analysis is used to optimize out unnecessary reference counting), unless it's marked otherwise with keywords such as __weak, __unsafe_unretained, etc.

Re: Garbage Collection is Wrong

#39
post #32
post #21

Earlier quoted context omitted.

For example, two lists both contain a reference to the same object. Neither one is a sole owner, and you can't safely destroy the object until both lists are destroyed.

Then the lists should only have a weak_ptr to the object. Something is handling both list [1], that something could own the object, or maybe something external to that. Giving ownership of the object to both list is a design error [2]. [1] e.g. if the two list are an implementation detail of a data structure, the data-structure itself could own the objects in the lists. [2] Do non-deterministic garbage collectors tha…

Sharing immutable data between multiple places with no single master owner is a perfectly reasonable thing to do in a program.

Re: Garbage Collection is Wrong

#40
Isn't it simply a form of garbage collection? Instead of garbage collecting all the unused objects at random moments, an object is garbage collected as soon as it goes out of scope.

The way I understand it, no garbage collection means you take care of cleaning after yourself, whereas garbage collectors do it for you. It sounds like what he calls resource acquisition.

Post reply on HN