0

Why some languages like C and C++ don't have garbage collection? I am used to Java, so I'm not sure what the benefits are of not having it?

Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
  • 12
    C# does have garbage collection. – juharr Jan 31 '17 at 03:05
  • 1
    You can read about garbage collection here: https://msdn.microsoft.com/en-us/library/0xy59wtx(v=vs.110).aspx – Girisha Jan 31 '17 at 03:05
  • Modern `c++` discourages the usage of new. Instead we use containers from the standard library and smart pointers. As a result of that the need for garbage collection is much less than it was 20+ years ago. – drescherjm Jan 31 '17 at 03:17
  • C++ has garbage collection too. We call it `std::shared_ptr`. Also, don't forget non-standard ones like [gcpp](https://github.com/hsutter/gcpp), [Boehm](http://www.hboehm.info/gc/), and C++/CLI in .NET Framework. –  Jan 31 '17 at 03:24
  • 1
    @NickyC, `shared_ptr` applies reference counting approach not garbage collection approach. – Ghasan غسان Jan 31 '17 at 03:30
  • @Ghasan Garbage collection is an umbrella term that may or may not include reference counting depending on context. –  Jan 31 '17 at 05:10
  • 1
    @NickyC Garbage collection means different thing. Reference counting and garbage collection are considered memory management approaches, but they mean and work differently. – Ghasan غسان Jan 31 '17 at 05:14
  • @Ghasan Google "reference counting garbage collection" and you will see search results that include reference counting to be garbage collection, and search results that do not. –  Jan 31 '17 at 05:24
  • Possible duplicate of [Java have Garbage collection and Why C and C++ languages not have garbage collection?](https://stackoverflow.com/questions/29789669/java-have-garbage-collection-and-why-c-and-c-languages-not-have-garbage-collec) – JdeBP Feb 08 '18 at 07:08

5 Answers5

8

Performance. There are generally two types of memory management for higher level languages. There is the garbage collection approach, and there is the reference counting found in Apple systems. Both of them incur a cost of performance when tracing objects and de-allocating them, and that has a footprint on the memory and CPU time needed.

Since C & C++ are old languages relatively, they were designed to fit with embedded systems and wide range of devices with many constraints, thus they could not afford to have a memory management.

With the right amount of practice and exposure, it should not be that difficult to manage C/C++ apps relatively well.

BTW, C# has a garbage collection. Only C and C++ don't have one.

Edit:

As the others might have added, in newer standards of C++, there is the shared pointer shared_ptr that applies reference counting approach to memory management.

Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
5

In addition to what other people have pointed out about performance (which is perfectly correct), I'd also like to point out that garbage collection is a major problem for real-time systems due to the unpredictability that that it introduces.

Recall that a real-time system is a system in which tasks must meet certain time constraints in order to be correct. For example, if you're writing code for a robot and it doesn't figure out that it's about to hit a wall in time to stop, then clearly that's not a correct result. The fact that you figured out that you're about to hit a wall after you've already done so is completely useless.

From the Microsoft documentation,

To reclaim objects, the garbage collector must stop all the executing threads in an application. In some situations, such as when an application retrieves data or displays content, a full garbage collection can occur at a critical time and impede performance.

This is particularly problematic since it can be very difficult or impossible to predict when the garbage collector is going to run or exactly how long it'll take to complete. This could potentially cause tasks to miss their deadline.

The article I linked to above describes in a lot more detail the performance implications of the garbage collector and ways you can minimize its impact in time-critical situations.

For what it's worth, there actually are efforts to make real-time versions of C# and Java. See, for example, Real Time Java.

I do realize that the vast majority of systems aren't real-time, but for the ones that are (engine controls, etc.) there's an obvious benefit for having a language without garbage collection.

4

Because it is more efficient that way. C* languages are geared towards:

  • producing as fast a binary program as possible
  • still be easy to understand for humans (so assembler, while faster, does not fulfill this requirement)

If you did automatic garbage collection, this takes time, and some times, it is not necessary for the program to run correctly. The programmer can decide on a case by case basis, if it is necessary or not.

Mark Galeck
  • 6,155
  • 1
  • 28
  • 55
3

C# does have garbage collection.

Garbage collection is really nice for programmers, but it requires a runtime cost. C/C++ are systems programming languages and, as such, they need to be able to run on bare metal - with as minimal a runtime as possible. This means that things like Garbage Collection are not possible.

Also, Garbage Collection can make it very difficult to reason about the memory consumption of your program. If you're designing, say, a real-time system responsible for keeping an airplane in the air, you don't want to risk a GC pause causing a catastrophic failure.

Kevin
  • 1,870
  • 2
  • 20
  • 22
3

There are a number of reasons, at least with respect to C:

  • C is a product of the early 1970s - note that older languages like Fortran, Cobol, Pascal, etc., don't have automatic garbage collection either;
  • C was derived from a systems programming language, and as such tends to not offer many high-level abstractions (pointers and streams are about as high level as it gets);
  • C and C++ are (typically) compiled to native binaries running directly on top of the OS (or even bare metal) - note that most languages that do automatic garbage collection run under a VM or an interpreter;
  • The philosophy of C is that the programmer is in the best position to know when a resource should be allocated or released, and is skilled enough to write the code to manage it;
  • As others have mentioned, AGC plays hell with performance-critical code;

EDIT

Note that automatic garbage collection has been around in some form since 1959, when McCarthy added it to Lisp. However, it didn't really become a thing in "mainstream" programming languages until the 1990s when Java came along; at that point, most systems were fast enough that we were willing to trade the (minor) performance hit for (somewhat) more robust code.

AGC can make life a lot easier in some respects, but it also introduces a small level of unpredictability in your code. RAII as usually practiced in C++ is a good compromise between all-manual memory management and automatic garbage collection. Done correctly, it gives you all the benefits of worry-free resource management and predictability.

John Bode
  • 119,563
  • 19
  • 122
  • 198