Live data from Hacker News

Garbage Collection is Wrong

lb-stuff.com

51–60 of 111 posts

Re: Garbage Collection is Wrong

#51
post #35
post #32

Earlier quoted context omitted.

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 co…

Have a vector of shared_ptr that own the objects in the graph and build a graph with weak_ptr ?

Removing an object is just as easy as removing an element from the vector. (If you test the weak_ptrs on use, that's actually the only thing you would need to do).

Re: Garbage Collection is Wrong

#53

As someone who's not interested in following every acronym and resource allocation strategy there is... is this even remotely useful? Lately I've gotten back into Java for Android, and after a very long time with Python, it's the first exposure in over a decade I've had to this argument... ...which, for some of us, isn't an argument, as we have basically no choice in the matter. But I'm still interested to know wheth…

I disagree. I believe even if you're stuck with a certain situation, it's important to know whether the status quo is actually a good thing and you should work to keep it or if it's unavoidable for the moment but generally bad and you should do your best to move away from it if you can.

Re: Garbage Collection is Wrong

#54
post #45

not even sure where to start... 1. plenty of garbage collectors do not pause all threads or wait for memory to be full. 2. you CAN ask for gc anytime you want in java and in C# (you need not wait for it to happen) 3. new and delete are still integral to C++ (check out any large codebase, like llvm)

1. Can you give an example of some of these systems? 2. Designing your code in such a way that requires it to invoke the GC seems counter-productive. If your algorithm is producing a bunch garbage that is statically known, why not just release that memory explicitly? Invoking the GC is way more expensive than necessary here. 3. New/delete will always be used in performance critical code but I think the point is that…

1. http://www.azulsystems.com/presentations/qcon-london-2011

2. fair but irrelevant. the argument was that there is no way to make GC happen. It was wrong. I never objected to the (perhaps more interesting) argument that one should be able to manually delete objects

3. then the parent should have said "in toy codebases, whose function is to look pretty, new and delete have no place" instead of a sweeping generalization that all uses of new and delete are archaic and wrong. That one generalization was an insult to every llvm & WebKit developer out there.

Disclaimer: i have no dog in this race - i prefer to work in C and think anyone who cannot free their own memory should maintain a safe 5 meter distance from any compiler. (this is my opinion only, of course)

Re: Garbage Collection is Wrong

#55
post #34
post #25

Earlier quoted context omitted.

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 comple…

Yes I think you would use a ref counted resource in that case. For your tangled mess of database connections I can't imagine ref counting not being adequate in those scenarios as well.

Re: Garbage Collection is Wrong

#56
post #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.

Garbage collection is not a synonym for automatic memory management.

Re: Garbage Collection is Wrong

#58
post #8

In modern C++, using new or delete in your code is wrong. It's not done. Nobody writes code like that anymore in C++. It's been quite a while since I wrote any significant C++ code, but this statement seems wrong. Is this really "state of the art" for C++?

You can get surprisingly far without using new or delete. std::vector, std::list and std::string are good replacements for many uses, you can of course create objects on the stack, and if your objects are stateless then they can maybe be const globals - and that can often save you a big pile of bother.

Though while new and delete are best avoided where possible, it's hardly the end of the world if you do use them. And my experience has always been that you're better off just using them, if the alternative is heavy use of smart pointers.

As member variables, smart pointers are tolerable, if copied to an ordinary pointer before heavy use; as locals or parameters, they're almost always best avoided. Over the years I've have had far more annoyance from painful single stepping, spitefully poor unoptimised performance and inconvenient watch window behaviour than I have ever had from just picking out all the memory leaks after the fact. (Which, if the code is written relatively tastefully, is usually pretty straightforward. And unless your colleagues are actively being dicks, I've always found there aren't too many to get rid of anyway.)

Re: Garbage Collection is Wrong

#59
post #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.

C++ has wider options managing resource acquisition. Scope is just one case.

https://en.wikipedia.org/wiki/Smart_pointer#C.2B.2B_smart_po...

Re: Garbage Collection is Wrong

#60

Earlier quoted context omitted.

Can you give an example of any codebase not using any new or delete at all that compares in size? "Right or Wrong" are irrelevant in the face of "Practical or Not". The fact that all large pieces of serious code use new and delete would either imply that the author is smarter than all the people writing that code combined, or (more likely) he did not consider something w.r.t. the practicality of not using them. Let m…

No but it is the trajectory of the future and it's very practical. I'm not saying there aren't oddball cases that will require new and delete, but for the most part, it simply isn't necessary. As for the citation, if you are immersed in the C++ community in general (going native, c++ now, subreddits, irc, accu meetings, etc), it's the general feeling.

Appeal To Anonymous Authority

This fallacy occurs whenever a person claims we should believe a proposition because it is also believed or claimed by some authority figure or figures — but in this case the authority is not named.

http://goo.gl/ZmRQ2x

Post reply on HN