6 ms·
Swift does have garbage collection! It uses reference counting, and AFAIK it doesn't have cycle detection, which means it's up to the developer to ensure that n
by david-given 10y ago
Swift does have garbage collection! It uses reference counting, and AFAIK it doesn't have cycle detection, which means it's up to the developer to ensure that no cycles occur.
Regarding responsive interfaces: pure reference counting is particularly problematic for this, because an unbounded amount of work may happen every time an object's reference is decremented (because the object might be freed, which causes other objects' references to become zero, etc). If this happens inside a critical section Your Life Is Pain. At least you can control when this happens by taking and then dropping an additional reference. (See Obj-C's allocation pools.)
C++'s shared_ptr<>, BTW, is also reference counting, and suffers from the same problem. So it's technically correct (which is the best kind of correct) to say that C++ has garbage collection too.
- WildUtah 10y agoReference counting isn't garbage collection. It's the opposite of garbage collection. With reference counting, which is optional in C++ and can be opted out of in Swift, you keep track of your allocations and release them when you're done with them. With garbage collection an outside program decides when you're done with memory and deals with it for you on its terms. It's true that there can be unlimited work done in reference counting. Of course, it's true there can be unlimited work in any function of any program. [0] With reference counting a programmer can fix the amount of work to be done and when in deterministic fashion. With garbage collection, there is no such power. The gc decides when it will work and you cannot control or predict the interruption. You cannot produce serious interactive or time sensitive control software like that. You certainly can't write system software like that. [0] https://en.wikipedia.org/wiki/Halting_problem https://en.wikipedia.org/wiki/Halting_problem
- lossolo 10y ago> Reference counting isn't garbage collection. "Reference counting is a form of garbage collection"[1] [1] https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Reference_counting https://en.wikipedia.org/wiki/Garbage_collection_(computer_s...
- prodigal_erik 10y agoIf the runtime mechanically detects that an object will never be used again, without you having to explicitly say so (and woe unto you if you're mistaken), that's what's meant by garbage collection. Reference counting does have a deterministic cost, but it's extremely high and applies to everything you do. Copying collection benefits from deferring and batching the work when most objects die quickly and can be ignored with zero effort (just recycle the arena containing all of them).
- WildUtah 10y agoYou can write a reference counter in three short lines of code that works the same as shared_ptr<T> or Swift. A modern automatic garbage collector requires a threading library plus tens of thousands of lines of intricate code. We're not comparing like with like here.
- prodigal_erik 10y agoThe three line version is really easy to write. And every time you assign to a pointer, it requires uncached atomic updates to the refcounts of the old and new objects, which is staggeringly expensive compared to a block of code that can go unused for minutes at a time. And all it gets you is learning that a small piece of memory is available slightly sooner, but you'll probably want to coalesce your free memory anyway for better locality.
- WildUtah 10y agoI hope that the 100-man years of top engineers version has some benefits over the 3-line newbie approach.
- lokedhs 10y agoPeople tend to think that refcounting must be faster than a tracing GC because you don't have to scan the memory. The second half of the previous sentence is true, but what they forget is that refcounting needs to do a lot of things that a GC doesn't have to: Like you said, need to update the reference every time it's moved. malloc becomes much slower, compared to a GC language (with a compacting GC) where malloc is effectively just a single add instruction. free also needs to do a lot of bookkeeping while the GC language doesn't need to do anything. Multithreading introduces another set of issues when dealing with refcounting that is not a problem with a GC.
- pjmlp 10y ago> Reference counting isn't garbage collection. Reference books in computer science, acknowledged by the experts in the field, happen to disagree. http://gchandbook.org/ http://gchandbook.org/
- khedoros1 10y agoHow about: Reference counting is one form of garbage collection, but "Garbage Collection" without any additional qualifiers or context usually implies "tracing garbage collection", with or without the aid of other GC algorithms. In common usage, I don't hear of languages like C++ described as a "garbage collecting language". That usually describes Java, Python, etc.
- pjmlp 10y agoC++ has a GC API and a few dialects that support GC like C++/CLI. Having RC library types doesn't make a language GC, because their correct use is not enforced by language semantics.
- mannykannot 10y agoI could say this in response to a dozen posts here, but arguing over the strict definition of the term is largely beside the point, which is the actual capabilities of the various languages. In fact, the argument itself shows that the distinction is no longer either as straightforward or useful as it once was, making the dichotomy moot.
- pjmlp 10y agoWell, those that review CS papers about GC algorithms to ACM, OOPSLA and SIGPLAN, think differently.
- mannykannot 10y agoElsewhere in this discussion: hellofunk: Then C++ is also garbage collected. yourself: Kind of, but only because C++11 defined a pluggable GC ABI. This is the sort of discussion that is far more fruitful than a pedantic argument over the precise meaning of a term of art in a field that is rapidly advancing. And if the academic community really is obsessed with this, then that is unfortunate.
- nostrademons 10y agoThey're duals, actually, and most practical GC algorithms used in production are hybrids of the two: http://www.cs.virginia.edu/~cs415/reading/bacon-garbage.pdf http://www.cs.virginia.edu/~cs415/reading/bacon-garbage.pdf
- WildUtah 10y agoThey're duals, actually No, actually, they're not. Just try allocating a lot of looped structure RAM and see.
- simias 10y agoI'm surprised by this heated debate regarding GC in this thread. Are you arguing that scripting languages that use reference counting are not garbage collected? PHP used reference counting for a long time (cycle detection only made it into the language in version 5.3 if I trust Wikipedia), it feels odd to consider PHP as not being garbage collected. For me the underlying technology is not very relevant, what matters is "do I have to worry about exactly when the resource is going to be dropped". I use the resource and when I stop using it it'll probably be deleted soon-ish. At any rate it's not really my problem (at least in a perfect world). You say "With reference counting [...] you keep track of your allocations and release them when you're done with them" but that's not really true, I don't keep track of anything, the language or library does. I can't really tell by looking at some code in isolation when or how the resource is destroyed. The GC takes care of that. Some languages like python, Java, Go or PHP have 1st party garbage collection that's active by default and other languages like C, C++, Rust and friends have optional 3rd party GC implemented as libraries (reference counting or otherwise). I really don't see what's so controversial about this.
- WildUtah 10y agoIn C, C++, Rust, Swift, &c I can determine how and when references go into or out of scope and get freed. The defining characteristic of a garbage collected system is an active outside process with priority and control over my code that chooses when to inject bugs (to wit: arbitrary pauses) into my program outside my control.
- braveo 10y agoPHP is a bad example, it was designed around the idea of the OS doing the cleanup for you when the process exited at the end of the request.
- leshow 10y agoWhen people say garbage collection they usually mean a tracing GC. You can twist words and say that shared_ptr means C++ has 'garbage collection' but I doubt anyone would agree with you if were were to word it like: "C++ has a garbage collector" Ref counting is a form of 'garbage collection' in so far as it is a way to release memory, that's about it. I certainly wouldn't consider Rust's Arc<T> a 'opt in garbage collector'.
- santaclaus 10y ago> When people say garbage collection they usually mean a tracing GC. Wikipedia lists both tracing and reference counting as forms of GC... hell, when interviewing at Microsoft I was asked to describe the two primary families of GC, expecting tracing and ref counting.
- WildUtah 10y agoAnyone can define a bat as a bird because it's an animal with wings that flies. But you'll soon find that isn't a useful conceptual distinction.
- pjmlp 10y agoWell, you are free to disagree with the computer science experts in the field of language design, but it won't make it less true.
- EdHominem 10y agoMore like, you're free to ignore the common-sense suggestion to examine the algorithms used as they pertain to your use case, just because someone once said "In theory these two things are equivalent". But it won't help you once you hit the inevitable edge case. Terminology is useful right up until it isn't. Resting on definitions rather than seeking proper descriptions is a great way to fail spectacularly.
- 10y ago