ChatGPT解决这个技术问题 Extra ChatGPT

Why doesn't C++ have a garbage collector?

I'm not asking this question because of the merits of garbage collection first of all. My main reason for asking this is that I do know that Bjarne Stroustrup has said that C++ will have a garbage collector at some point in time.

With that said, why hasn't it been added? There are already some garbage collectors for C++. Is this just one of those "easier said than done" type things? Or are there other reasons it hasn't been added (and won't be added in C++11)?

Cross links:

Garbage collectors for C++

Just to clarify, I understand the reasons why C++ didn't have a garbage collector when it was first created. I'm wondering why the collector can't be added in.

This is one of the top ten myths about C++ that the haters always bring up. Garbage collection isn't "built in", but there are several easy ways to do it C++. Posting a comment because others have already answered better than I could below :)
But that's the whole point about not being built-in, you have to do it yourself. Realibility from high to low : built-in, library, home-made. I use C++ myself, and definitely not a hater because it's the best language in the world. But dynamic memory management is a pain.
@Davr - I'm not a C++ hater nor am I even trying to even argue that C++ needs a garbage collector. I'm asking because I know that Bjarne Stroustrup has said that it WILL be added and was just curious what the reasons for not implementing it were.
This article The Boehm Collector for C and C++ from Dr. Dobbs describes an open source garbage collector that can be used with both C and C++. It discusses some of the issues that arise with using a garbage collector with C++ destructors as well as the C Standard Library.
@rogerdpack: But it's not that useful by now (see my answer...) so it's unlikely implementations will invest in having one.

1
10 revs, 3 users 90%

Implicit garbage collection could have been added in, but it just didn't make the cut. Probably due to not just implementation complications, but also due to people not being able to come to a general consensus fast enough.

A quote from Bjarne Stroustrup himself:

I had hoped that a garbage collector which could be optionally enabled would be part of C++0x, but there were enough technical problems that I have to make do with just a detailed specification of how such a collector integrates with the rest of the language, if provided. As is the case with essentially all C++0x features, an experimental implementation exists.

There is a good discussion of the topic here.

General overview:

C++ is very powerful and allows you to do almost anything. For this reason it doesn't automatically push many things onto you that might impact performance. Garbage collection can be easily implemented with smart pointers (objects that wrap pointers with a reference count, which auto delete themselves when the reference count reaches 0).

C++ was built with competitors in mind that did not have garbage collection. Efficiency was the main concern that C++ had to fend off criticism from in comparison to C and others.

There are 2 types of garbage collection...

Explicit garbage collection:

C++0x has garbage collection via pointers created with shared_ptr

If you want it you can use it, if you don't want it you aren't forced into using it.

For versions before C++0x, boost:shared_ptr exists and serves the same purpose.

Implicit garbage collection:

It does not have transparent garbage collection though. It will be a focus point for future C++ specs though.

Why Tr1 doesn't have implicit garbage collection?

There are a lot of things that tr1 of C++0x should have had, Bjarne Stroustrup in previous interviews stated that tr1 didn't have as much as he would have liked.


I would become a hater if C++ forced garbage collection upon me! Why can't people use smart_ptr's? How would you do low level Unix style forking, with a garbage collector in the way? Other things would be affected such as threading. Python has its global interpreter lock mostly because of it's garbage collection (see Cython). Keep it out of C / C++, thanks.
@unixman83: The main problem with reference counted garbage collection (ie std::shared_ptr) is cyclical references, which cause a memory leak. Therefore you must carefully use std::weak_ptr to break cycles, which is messy. Mark and sweep style GC does not have this problem. There is no inherent incompatibility between threading/forking and garbage collection. Java and C# both have high performance preemptive multithreading and and a garbage collector. There are issues to do with realtime applications and a garbage collector, as most garbage collectors have to stop the world to run.
"The main problem with reference counted garbage collection (ie std::shared_ptr) is cyclical references" and awful performance which is ironic because better performance is usually the justification for using C++... flyingfrogblog.blogspot.co.uk/2011/01/…
"How would you do low level Unix style forking". The same way GC'd languages like OCaml have been doing it for ~20 years or more.
"Python has its global interpreter lock mostly because of it's garbage collection". Strawman argument. Java and .NET both have GCs but neither have global locks.
M
MaksymB

To add to the debate here.

There are known issues with garbage collection, and understanding them helps understanding why there is none in C++.

1. Performance ?

The first complaint is often about performance, but most people don't really realize what they are talking about. As illustrated by Martin Beckett the problem may not be performance per se, but the predictability of performance.

There are currently 2 families of GC that are widely deployed:

Mark-And-Sweep kind

Reference-Counting kind

The Mark And Sweep is faster (less impact on overall performance) but it suffers from a "freeze the world" syndrome: i.e. when the GC kicks in, everything else is stopped until the GC has made its cleanup. If you wish to build a server that answers in a few milliseconds... some transactions will not live up to your expectations :)

The problem of Reference Counting is different: reference-counting adds overhead, especially in Multi-Threading environments because you need to have an atomic count. Furthermore there is the problem of reference cycles so you need a clever algorithm to detect those cycles and eliminate them (generally implement by a "freeze the world" too, though less frequent). In general, as of today, this kind (even though normally more responsive or rather, freezing less often) is slower than the Mark And Sweep.

I have seen a paper by Eiffel implementers that were trying to implement a Reference Counting Garbage Collector that would have a similar global performance to Mark And Sweep without the "Freeze The World" aspect. It required a separate thread for the GC (typical). The algorithm was a bit frightening (at the end) but the paper made a good job of introducing the concepts one at a time and showing the evolution of the algorithm from the "simple" version to the full-fledged one. Recommended reading if only I could put my hands back on the PDF file...

2. Resources Acquisition Is Initialization (RAII)

It's a common idiom in C++ that you will wrap the ownership of resources within an object to ensure that they are properly released. It's mostly used for memory since we don't have garbage collection, but it's also useful nonetheless for many other situations:

locks (multi-thread, file handle, ...)

connections (to a database, another server, ...)

The idea is to properly control the lifetime of the object:

it should be alive as long as you need it

it should be killed when you're done with it

The problem of GC is that if it helps with the former and ultimately guarantees that later... this "ultimate" may not be sufficient. If you release a lock, you'd really like that it be released now, so that it does not block any further calls!

Languages with GC have two work arounds:

don't use GC when stack allocation is sufficient: it's normally for performance issues, but in our case it really helps since the scope defines the lifetime

using construct... but it's explicit (weak) RAII while in C++ RAII is implicit so that the user CANNOT unwittingly make the error (by omitting the using keyword)

3. Smart Pointers

Smart pointers often appear as a silver bullet to handle memory in C++. Often times I have heard: we don't need GC after all, since we have smart pointers.

One could not be more wrong.

Smart pointers do help: auto_ptr and unique_ptr use RAII concepts, extremely useful indeed. They are so simple that you can write them by yourself quite easily.

When one need to share ownership however it gets more difficult: you might share among multiple threads and there are a few subtle issues with the handling of the count. Therefore, one naturally goes toward shared_ptr.

It's great, that's what Boost for after all, but it's not a silver bullet. In fact, the main issue with shared_ptr is that it emulates a GC implemented by Reference Counting but you need to implement the cycle detection all by yourself... Urg

Of course there is this weak_ptr thingy, but I have unfortunately already seen memory leaks despite the use of shared_ptr because of those cycles... and when you are in a Multi Threaded environment, it's extremely difficult to detect!

4. What's the solution ?

There is no silver bullet, but as always, it's definitely feasible. In the absence of GC one need to be clear on ownership:

prefer having a single owner at one given time, if possible

if not, make sure that your class diagram does not have any cycle pertaining to ownership and break them with subtle application of weak_ptr

So indeed, it would be great to have a GC... however it's no trivial issue. And in the mean time, we just need to roll up our sleeves.


I wish I could accept two answers! This is just great. One thing to point out, in regards to performance, the GC that runs in a separate thread is actually pretty common (it's used in Java and .Net). Granted, that might not be acceptable in embedded systems.
Only two types? How 'bout copying collectors? Generational collectors? Assorted concurrent collectors (including Baker's hard real-time treadmill)? Various hybrid collectors? Man, the sheer ignorance in the industry of this field astonishes me sometimes.
Did I say there were only 2 types ? I said that there were 2 that were widely deployed. As far as I know Python, Java and C# all use Mark and Sweep algorithms now (Java used to have a reference counting algorithm). To be even more precise, it seems to me than C# uses Generational GC for minor cycles, Mark And Sweep for major cycles and Copying to fight off memory fragmentation; though I would argue that the heart of the algorithm is Mark And Sweep. Do you know any mainstream language that uses another technology ? I'm always happy to learn.
You just named one mainstream language that used three.
Main difference is that Generational and Incremental GC do not need to stop the world to work, and you can make them work on single-threaded systems without too much overhead by occasionally performing a iterations of the tree traversal when accessing the GC pointers (the factor can be determined by the number of new nodes, along with a basic prediction of the need to collect). You can take GC even further by including data about where in code the creation/modification of the node occurred, which could allow you to improve your predictions, and you get Escape Analysis for free with it.
M
Martin Beckett

What type? should it be optimised for embedded washing machine controllers, cell phones, workstations or supercomputers? Should it prioritise gui responsiveness or server loading? should it use lots of memory or lots of CPU?

C/c++ is used in just too many different circumstances. I suspect something like boost smart pointers will be enough for most users

Edit - Automatic garbage collectors aren't so much a problem of performance (you can always buy more server) it's a question of predicatable performance. Not knowing when the GC is going to kick in is like employing a narcoleptic airline pilot, most of the time they are great - but when you really need responsiveness!


I definitely see your point, but I feel compelled to ask: isn't Java used in just about as many applications?
No. Java is not suitable for high performance applications, for the simple reason that it doesn't have performance guarantees to the same extent as C++. So you'll find it in a cell phone, but you won't find it in a cell switch or supercomputer.
You can always buy more server, but you can't always buy more CPU for the cell phone already in the customer's pocket!
Java has done a lot of performance catchup in CPU efficiency. The really intractable problem is memory usage, Java is inherently less memory efficient than C++. And that inefficiency is due to the fact that it is garbage collected. Garbage collection cannot be both fast and memory efficient, a fact that becomes obvious if you look into how fast GC algorithms work.
@Zathrus java can win on throughput b/c of the optimizing jit, though not latency (boo real-time), and certainly not memory footprint.
G
Greg Rogers

One of the biggest reasons that C++ doesn't have built in garbage collection is that getting garbage collection to play nice with destructors is really, really hard. As far as I know, nobody really knows how to solve it completely yet. There are alot of issues to deal with:

deterministic lifetimes of objects (reference counting gives you this, but GC doesn't. Although it may not be that big of a deal).

what happens if a destructor throws when the object is being garbage collected? Most languages ignore this exception, since theres really no catch block to be able to transport it to, but this is probably not an acceptable solution for C++.

How to enable/disable it? Naturally it'd probably be a compile time decision but code that is written for GC vs code that is written for NOT GC is going to be very different and probably incompatible. How do you reconcile this?

These are just a few of the problems faced.


GC and destructors is a solved problem, by a nice sidestep from Bjarne. Destructors don't run during GC, because that's not the point of GC. GC in C++ exists to create the notion of infinite memory, not infinite other resources.
If destructors don't run that completely changes the semantics of the language. I guess at the very least you'd need a new keyword "gcnew" or something so that you explicitly allow this object to be GC'ed (and therefore you shouldn't use it to wrap resources besides memory).
This is a bogus argument. Since C++ has explicit memory management, you need to figure out when every object must be freed. With GC, it is no worse; rather, the problem is reduced to figuring out when certain objects are freed, namely those objects that require special considerations upon deletion. Experience programming in Java and C# reveals that the vast majority of objects require no special considerations and can be safely left to the GC. As it turns out, one of the main functions of destructors in C++ is to free child objects, which GC handles for you automatically.
@NateC-K: One thing which is improved in GC vs non-GC (perhaps the biggest thing) is the ability a solid GC system to guarantee that every reference will continue to point to the same object as long as the reference exists. Calling Dispose on an object may make it unsable, but references which pointed to the object when it was alive will continue to do so after it's dead. By contrast, in non-GC systems, objects can be deleted while references exist, and there's seldom any limit to the havoc that may be wreaked if one of those references gets used.
J
Jerry Coffin

Though this is an old question, there's still one problem that I don't see anybody having addressed at all: garbage collection is almost impossible to specify.

In particular, the C++ standard is quite careful to specify the language in terms of externally observable behavior, rather than how the implementation achieves that behavior. In the case of garbage collection, however, there is virtually no externally observable behavior.

The general idea of garbage collection is that it should make a reasonable attempt at assuring that a memory allocation will succeed. Unfortunately, it's essentially impossible to guarantee that any memory allocation will succeed, even if you do have a garbage collector in operation. This is true to some extent in any case, but particularly so in the case of C++, because it's (probably) not possible to use a copying collector (or anything similar) that moves objects in memory during a collection cycle.

If you can't move objects, you can't create a single, contiguous memory space from which to do your allocations -- and that means your heap (or free store, or whatever you prefer to call it) can, and probably will, become fragmented over time. This, in turn, can prevent an allocation from succeeding, even when there's more memory free than the amount being requested.

While it might be possible to come up with some guarantee that says (in essence) that if you repeat exactly the same pattern of allocation repeatedly, and it succeeded the first time, it will continue to succeed on subsequent iterations, provided that the allocated memory became inaccessible between iterations. That's such a weak guarantee it's essentially useless, but I can't see any reasonable hope of strengthening it.

Even so, it's stronger than what has been proposed for C++. The previous proposal [warning: PDF] (that got dropped) didn't guarantee anything at all. In 28 pages of proposal, what you got in the way of externally observable behavior was a single (non-normative) note saying:

[ Note: For garbage collected programs, a high quality hosted implementation should attempt to maximize the amount of unreachable memory it reclaims. —end note ]

At least for me, this raises a serious question about return on investment. We're going to break existing code (nobody's sure exactly how much, but definitely quite a bit), place new requirements on implementations and new restrictions on code, and what we get in return is quite possibly nothing at all?

Even at best, what we get are programs that, based on testing with Java, will probably require around six times as much memory to run at the same speed they do now. Worse, garbage collection was part of Java from the beginning -- C++ places enough more restrictions on the garbage collector that it will almost certainly have an even worse cost/benefit ratio (even if we go beyond what the proposal guaranteed and assume there would be some benefit).

I'd summarize the situation mathematically: this a complex situation. As any mathematician knows, a complex number has two parts: real and imaginary. It appears to me that what we have here are costs that are real, but benefits that are (at least mostly) imaginary.


I would posit that even if one specifies that for proper operation all objects must be deleted, and only objects which had been deleted would be eligible for collection, compiler support for reference-tracking garbage collection could still be useful, since such a language could ensure that use of a deleted pointer (reference) would be guaranteed to trap, rather than causing Undefined Behavior.
Even in Java, the GC is not really specified to do anything useful AFAIK. It might call free for you (where I mean free analagous to the C language). But Java never guarantees to call finalizers or anything like that. In fact, C++ does much more than Java to run around commit database writes, flushing file handles, and so on. Java claims to have "GC", but Java developers have to meticulously call close() all the time and they have to be very aware of resource management, being careful not to call close() too soon or too late. C++ frees us from that. ...(continued)
.. my comment a moment ago is not intended to criticise Java. I'm just observing that the term "garbage collection" is a very weird term - it means much less than people think it does and therefore it's difficult to discuss it without being clear what it means.
@AaronMcDaid It's true that GC does not help with non-memory resources at all. Luckily, such resources get allocated pretty rarely when compared to memory. Moreover, more than 90% of them can be freed in the method which allocated them, so try (Whatever w=...) {...} solves it (and you get a warning when you forget). The remaining ones are problematic with RAII, too. Calling close() "all the time" means maybe once per tens of thousand lines, so that's not that bad, while memory gets allocated nearly on every Java line.
A
Andriy Makukha

If you want automatic garbage collection, there are good commercial and public-domain garbage collectors for C++. For applications where garbage collection is suitable, C++ is an excellent garbage collected language with a performance that compares favorably with other garbage collected languages. See The C++ Programming Language (4rd Edition) for a discussion of automatic garbage collection in C++. See also, Hans-J. Boehm's site for C and C++ garbage collection (archive). Also, C++ supports programming techniques that allow memory management to be safe and implicit without a garbage collector. I consider garbage collection a last choice and an imperfect way of handling for resource management. That does not mean that it is never useful, just that there are better approaches in many situations.

Source: http://www.stroustrup.com/bs_faq.html#garbage-collection

As for why it doesnt have it built in, If I remember correctly it was invented before GC was the thing, and I don't believe the language could have had GC for several reasons(I.E Backwards compatibilty with C)

Hope this helps.


"with a performance that compares favorably with other garbage collected languages". Citation?
My link was broken. I wrote this answer 5 years ago.
Ok, I was hoping for some independent verification of these claims, i.e. not by Stroustrup or Boehm. :-)
A
Aaron McDaid

Stroustrup made some good comments on this at the 2013 Going Native conference.

Just skip to about 25m50s in this video. (I'd recommend watching the whole video actually, but this skips to the stuff about garbage collection.)

When you have a really great language that makes it easy (and safe, and predictable, and easy-to-read, and easy-to-teach) to deal with objects and values in a direct way, avoiding (explicit) use of the heap, then you don't even want garbage collection.

With modern C++, and the stuff we have in C++11, garbage collection is no longer desirable except in limited circumstances. In fact, even if a good garbage collector is built into one of the major C++ compilers, I think that it won't be used very often. It will be easier, not harder, to avoid the GC.

He shows this example:

void f(int n, int x) {
    Gadget *p = new Gadget{n};
    if(x<100) throw SomeException{};
    if(x<200) return;
    delete p;
}

This is unsafe in C++. But it's also unsafe in Java! In C++, if the function returns early, the delete will never be called. But if you had full garbage collection, such as in Java, you merely get a suggestion that the object will be destructed "at some point in the future" (Update: it's even worse that this. Java does not promise to call the finalizer ever - it maybe never be called). This isn't good enough if Gadget holds an open file handle, or a connection to a database, or data which you have buffered for write to a database at a later point. We want the Gadget to be destroyed as soon as it's finished, in order to free these resources as soon as possible. You don't want your database server struggling with thousands of database connections that are no longer needed - it doesn't know that your program is finished working.

So what's the solution? There are a few approaches. The obvious approach, which you'll use for the vast majority of your objects is:

void f(int n, int x) {
    Gadget p = {n};  // Just leave it on the stack (where it belongs!)
    if(x<100) throw SomeException{};
    if(x<200) return;
}

This takes fewer characters to type. It doesn't have new getting in the way. It doesn't require you to type Gadget twice. The object is destroyed at the end of the function. If this is what you want, this is very intuitive. Gadgets behave the same as int or double. Predictable, easy-to-read, easy-to-teach. Everything is a 'value'. Sometimes a big value, but values are easier to teach because you don't have this 'action at a distance' thing that you get with pointers (or references).

Most of the objects you make are for use only in the function that created them, and perhaps passed as inputs to child functions. The programmer shouldn't have to think about 'memory management' when returning objects, or otherwise sharing objects across widely separated parts of the software.

Scope and lifetime are important. Most of the time, it's easier if the lifetime is the same as the scope. It's easier to understand and easier to teach. When you want a different lifetime, it should be obvious reading the code that you're doing this, by use of shared_ptr for example. (Or returning (large) objects by value, leveraging move-semantics or unique_ptr.

This might seem like an efficiency problem. What if I want to return a Gadget from foo()? C++11's move semantics make it easier to return big objects. Just write Gadget foo() { ... } and it will just work, and work quickly. You don't need to mess with && yourself, just return things by value and the language will often be able to do the necessary optimizations. (Even before C++03, compilers did a remarkably good job at avoiding unnecessary copying.)

As Stroustrup said elsewhere in the video (paraphrasing): "Only a computer scientist would insist on copying an object, and then destroying the original. (audience laughs). Why not just move the object directly to the new location? This is what humans (not computer scientists) expect."

When you can guarantee only one copy of an object is needed, it's much easier to understand the lifetime of the object. You can pick what lifetime policy you want, and garbage collection is there if you want. But when you understand the benefits of the other approaches, you'll find that garbage collection is at the bottom of your list of preferences.

If that doesn't work for you, you can use unique_ptr, or failing that, shared_ptr. Well written C++11 is shorter, easier-to-read, and easier-to-teach than many other languages when it comes to memory management.


GC should only be used for objects that don't acquire resources (i.e. ask other entities to do things on their behalf "until further notice"). If Gadget doesn't ask anything else to do anything on its behalf, the original code would be perfectly safe in Java if the meaningless (to Java) delete statement were removed.
@supercat, objects with boring destructors are interesting. (I haven't defined 'boring', but basically destructors that never need to be called, except for the freeing of memory). It might be possible for an individual compiler to treat shared_ptr<T> specially when T is 'boring'. It could decide to not actually manage a ref counter for that type, and instead to use GC. This would allow GC to be used without the developer needing to notice. A shared_ptr could simply be seen as a GC pointer, for suitable T. But there are limitations in this, and it would make many programs slower.
A good type system should have different types for GC and RAII-managed heap objects, since some usage patterns work very well with one and very poorly with the other. In .NET or Java, a statement string1=string2; will execute very quickly regardless of the length of the string (it's literally nothing more than a register load and register store), and does not require any locking to ensure that if the above statement is executed while string2 is being written, string1 will hold either the old value or the new value, with no Undefined Behavior).
In C++, assignment of a shared_ptr<String> requires a lot of behind-the-scenes synchronization, and assignment of a String may behave oddly if a variable is read and written simultaneously. Cases where one would want to write and read a String simultaneously aren't terribly common, but can arise if e.g. some code wishes to make ongoing status reports available to other threads. In .NET and Java, such things just "work".
@curiousguy nothing has changed, unless you take the right precautions, Java still allows to the finalizer to be called as soon as the constructor has finished. Here a real life example: “finalize() called on strongly reachable objects in Java 8”. The conclusion is to never use this feature, that almost everyone agrees to be a historical design mistake of the language. When we follow that advice, the language provides the determinism we love.
e
einpoklum

tl;dr: Because modern C++ doesn't need garbage collection.

Bjarne Stroustrup's FAQ answer on this matter says:

I don't like garbage. I don't like littering. My ideal is to eliminate the need for a garbage collector by not producing any garbage. That is now possible.

The situation, for code written these days (C++17 and following the official Core Guidelines) is as follows:

Most memory ownership-related code is in libraries (especially those providing containers).

Most use of code involving memory ownership follows the CADRe or RAII pattern, so allocation is made on construction and deallocation on destruction, which happens when exiting the scope in which something was allocated.

You do not explicitly allocate or deallocate memory directly.

Raw pointers do not own memory (if you've followed the guidelines), so you can't leak by passing them around.

If you're wondering how you're going to pass the starting addresses of sequences of values in memory - you can and should prefer span's, obviating the need for raw pointers. You can still use such pointers, they'll just be non-owning.

If you really need an owning "pointer", you use C++' standard-library smart pointers - they can't leak, and are decently efficient (although the ABI can get in the way of that). Alternatively, you can pass ownership across scope boundaries with "owner pointers". These are uncommon and must be used explicitly; but when adopted - they allow for nice static checking against leaks.

"Oh yeah? But what about...

... if I just write code the way we used to write C++ in the old days?"

Indeed, you could just disregard all of the guidelines and write leaky application code - and it will compile and run (and leak), same as always.

But it's not a "just don't do that" situation, where the developer is expected to be virtuous and exercise a lot of self control; it's just not simpler to write non-conforming code, nor is it faster to write, nor is it better-performing. Gradually it will also become more difficult to write, as you would face an increasing "impedance mismatch" with what conforming code provides and expects.

... if I reintrepret_cast? Or do complex pointer arithmetic? Or other such hacks?"

Indeed, if you put your mind to it, you can write code that messes things up despite playing nice with the guidelines. But:

You would do this rarely (in terms of places in the code, not necessarily in terms of fraction of execution time) You would only do this intentionally, not accidentally. Doing so will stand out in a codebase conforming to the guidelines. It's the kind of code in which you would bypass the GC in another language anyway.

... library development?"

If you're a C++ library developer then you do write unsafe code involving raw pointers, and you are required to code carefully and responsibly - but these are self-contained pieces of code written by experts (and more importantly, reviewed by experts).

So, it's just like Bjarne said: There's really no motivation to collect garbage generally, as you all but make sure not to produce garbage. GC is becoming a non-problem with C++.

That is not to say GC isn't an interesting problem for certain specific applications, when you want to employ custom allocation and de-allocations strategies. For those you would want custom allocation and de-allocation, not a language-level GC.


Well, it does (need G C) if you are grinding strings .. Imagine you have large string arrays (think hundreds of megabytes) that you are building piecemeal, then processing and rebuilding into different lengths, deleting unused ones, combining others etc. I know because I have had to switch to high level languages to cope. (Of course you could build your own G C as well).
@user1863152 : That's a case in which a custom allocator would be useful. It still doesn't necessitate a language-integral GC...
to einpoklum : true. It's just horse for courses. My requirement was to process dynamically changing gallons of Transport Passenger Information. Fascinating subject .. Really comes down to software philosophy.
GC as the Java and .NET world have discovered finally has a massive problem - it does not scale. When you have billions of live objects in memory as we do these days with any non trivial software, you'll have to start writing code to hide things from the GC. It's a burden to have GC in Java and .NET.
@ZachSaw: What fraction of programs would ever have even one billion live objects in memory? You're saying all programs that don't have more than that are trivial?
U
Uri

The idea behind C++ was that you would not pay any performance impact for features that you don't use. So adding garbage collection would have meant having some programs run straight on the hardware the way C does and some within some sort of runtime virtual machine.

Nothing prevents you from using some form of smart pointers that are bound to some third-party garbage collection mechanism. I seem to recall Microsoft doing something like that with COM and it didn't go to well.


I don't think GC requires a VM. The compiler could add code to all pointer operations to update a global state, while a separate thread runs in the background deleting objects as needed.
I agree. You don't need a virtual machine, but the second you start having something manage your memory for you like that in the background, my feel is that you've left the actual "electric wires" and have sort of a VM situation.
N
Nemanja Trifunovic

To answer most "why" questions about C++, read Design and Evolution of C++


s
supercat

One of the fundamental principles behind the original C language is that memory is composed of a sequence of bytes, and code need only care about what those bytes mean at the exact moment that they are being used. Modern C allows compilers to impose additional restrictions, but C includes--and C++ retains--the ability to decompose a pointer into a sequence of bytes, assemble any sequence of bytes containing the same values into a pointer, and then use that pointer to access the earlier object.

While that ability can be useful--or even indispensable--in some kinds of applications, a language that includes that ability will be very limited in its ability to support any kind of useful and reliable garbage collection. If a compiler doesn't know everything that has been done with the bits that made up a pointer, it will have no way of knowing whether information sufficient to reconstruct the pointer might exist somewhere in the universe. Since it would be possible for that information to be stored in ways that the computer wouldn't be able to access even if it knew about them (e.g. the bytes making up the pointer might have been shown on the screen long enough for someone to write them down on a piece of paper), it may be literally impossible for a computer to know whether a pointer could possibly be used in the future.

An interesting quirk of many garbage-collected frameworks is that an object reference not defined by the bit patterns contained therein, but by the relationship between the bits held in the object reference and other information held elsewhere. In C and C++, if the bit pattern stored in a pointer identifies an object, that bit pattern will identify that object until the object is explicitly destroyed. In a typical GC system, an object may be represented by a bit pattern 0x1234ABCD at one moment in time, but the next GC cycle might replace all references to 0x1234ABCD with references to 0x4321BABE, whereupon the object would be represented by the latter pattern. Even if one were to display the bit pattern associated with an object reference and then later read it back from the keyboard, there would be no expectation that the same bit pattern would be usable to identify the same object (or any object).


That is a really good point, I just recently stole some bits from my pointers because otherwise there would be stupid amounts of cache misses.
@PasserBy: I wonder how many applications that use 64-bit pointers would benefit more from either using scaled 32-bit pointers as object references, or else keeping almost everything in 4GiB of address space and using special objects to store/retrieve data from high-speed storage beyond? Machines have enough RAM that the RAM consumption of 64-bit pointers might not matter, except that they gobble twice as much cache as 32-bit pointers.
S
Sohail Si

SHORT ANSWER: We don't know how to do garbage collection efficiently (with minor time and space overhead) and correctly all the time (in all possible cases).

LONG ANSWER: Just like C, C++ is a systems language; this means it is used when you are writing system code, e.g., operating system. In other words, C++ is designed, just like C, with best possible performance as the main target. The language' standard will not add any feature that might hinder the performance objective.

This pauses the question: Why garbage collection hinders performance? The main reason is that, when it comes to implementation, we [computer scientists] do not know how to do garbage collection with minimal overhead, for all cases. Hence it's impossible to the C++ compiler and runtime system to perform garbage collection efficiently all the time. On the other hand, a C++ programmer, should know his design/implementation and he's the best person to decide how to best do the garbage collection.

Last, if control (hardware, details, etc.) and performance (time, space, power, etc.) are not the main constraints, then C++ is not the right tool. Other language might serve better and offer more [hidden] runtime management, with the necessary overhead.


Best answer. It's the only answer that points to the main reason and the root cause.
B
Bob Holmes

All the technical talking is overcomplicating the concept.

If you put GC into C++ for all the memory automatically then consider something like a web browser. The web browser must load a full web document AND run web scripts. You can store web script variables in the document tree. In a BIG document in a browser with lots of tabs open, it means that every time the GC must do a full collection it must also scan all the document elements.

On most computers this means that PAGE FAULTS will occur. So the main reason, to answer the question is that PAGE FAULTS will occur. You will know this as when your PC starts making lots of disk access. This is because the GC must touch lots of memory in order to prove invalid pointers. When you have a bona fide application using lots of memory, having to scan all objects every collection is havoc because of the PAGE FAULTS. A page fault is when virtual memory needs to get read back into RAM from disk.

So the correct solution is to divide an application into the parts that need GC and the parts that do not. In the case of the web browser example above, if the document tree was allocated with malloc, but the javascript ran with GC, then every time the GC kicks in it only scans a small portion of memory and all PAGED OUT elements of the memory for the document tree does not need to get paged back in.

To further understand this problem, look up on virtual memory and how it is implemented in computers. It is all about the fact that 2GB is available to the program when there is not really that much RAM. On modern computers with 2GB RAM for a 32BIt system it is not such a problem provided only one program is running.

As an additional example, consider a full collection that must trace all objects. First you must scan all objects reachable via roots. Second scan all the objects visible in step 1. Then scan waiting destructors. Then go to all the pages again and switch off all invisible objects. This means that many pages might get swapped out and back in multiple times.

So my answer to bring it short is that the number of PAGE FAULTS which occur as a result of touching all the memory causes full GC for all objects in a program to be unfeasible and so the programmer must view GC as an aid for things like scripts and database work, but do normal things with manual memory management.

And the other very important reason of course is global variables. In order for the collector to know that a global variable pointer is in the GC it would require specific keywords, and thus existing C++ code would not work.


M
Mike76

When we compare C++ with Java, we see that C++ was not designed with implicit Garbage Collection in mind, while Java was.

Having things like arbitrary pointers in C-Style is not only bad for GC-implementations, but it would also destroy backward compatibility for a large amount of C++-legacy-code.

In addition to that, C++ is a language that is intended to run as standalone executable instead of having a complex run-time environment.

All in all: Yes it might be possible to add Garbage Collection to C++, but for the sake of continuity it is better not to do so.


Freeing memory and running destructors are too completely separate issues. (Java doesn't have destructors, which is a PITA.) GC frees memory, it doesn't run dtors.
M
Marc Coll

Mainly for two reasons:

Because it doesn't need one (IMHO) Because it's pretty much incompatible with RAII, which is the cornerstone of C++

C++ already offers manual memory management, stack allocation, RAII, containers, automatic pointers, smart pointers... That should be enough. Garbage collectors are for lazy programmers who don't want to spend 5 minutes thinking about who should own which objects or when should resources be freed. That's not how we do things in C++.


There are numerous (newer) algorithms which are inherently difficult to implement without garbage collection. Time moved on. Innovation also comes from new insights which match well to (garbage collecting) high level languages. Try to backport any of these to GC free C++, you will notice the bumps in the road. (I know I should give examples, but I am kind of in a hurry right now. Sorry. One I can think of right now revolves around persistent data structures, where reference counting won't work.).
w
www-0av-Com

Imposing garbage collection is really a low level to high level paradigm shift.

If you look at the way strings are handled in a language with garbage collection, you will find they ONLY allow high level string manipulation functions and do not allow binary access to the strings. Simply put, all string functions first check the pointers to see where the string is, even if you are only drawing out a byte. So if you are doing a loop that processes each byte in a string in a language with garbage collection, it must compute the base location plus offset for each iteration, because it cannot know when the string has moved. Then you have to think about heaps, stacks, threads, etc etc.