Why modern C++ doesn’t need a garbage collector

Old 1 Comment on Why modern C++ doesn’t need a garbage collector 53

Let’s see that how modern C++ has reinforced itself as the de facto language when it comes to raw performance, one of the factors being no garbage collection. Yes, you read it right, C++ doesn’t require a garbage collection mechanism.

In my previous posts, I wrote about the few important and very useful features of modern C++, that is, C++11 and above. Now coming to one of the important features whose intricacies every C++ programmer should know so as to get the most out of the raw performance C++ has to offer.

Programmers like to favor languages that handle all the dirty work for them (garbage collection is dirty after all!). Ask a programmer using one of the newer generation languages and one of the reasons the programmer might give to support their language will often be garbage collection.

With garbage collectors backing you up, you do not need to worry dropping orphaned objects behind you as they will be taken care of. However, C++ didn’t have that capability earlier which used to often crash software due to excessive memory leaks. But the modern C++ now has mechanism for ensuring that whatever garbage you might forget, will be taken care of very soon.

Typical garbage collectors are separate processes which monitor the memory being used and tracks references to variables. When the reference count reaches zero, the variables are automatically deleted, thus freeing up memory. However, these processes take up some amount of CPU cycles, which might be significant for CPU intensive tasks. And what if the garbage collector decides to collect the garbage right when your application is performing a CPU intensive job? Bummer.

C++ has the concept of Resource Acquisition Is Initialization (RAII) which basically means that objects acquire required resources upon initialization and also release them upon destruction.

RAII can be used in C++ by making use of smart pointers like auto_ptr, shared_ptr or weak_ptr. These are objects which hold pointers of your objects while initialization and automatically destruct them when their scope ends, typically on encountering a closing brace.

Most programmers build C++ classes properly, but may not manage them properly, which may lead to resource leaks. Using naked pointers often lead to some objects to be overlooked and are never deleted, thus occupying memory. Wrapping object pointers with smart pointers ensures that whenever the scope of the object ends, it is destructed properly by calling the appropriate destructors.

So the moral of the story goes as –

closing braces and smart pointers are your best friends in C++

This feature is very useful for those programming electronics hardware, like Arduino, which supports C++ code, as embedded systems are more prone to consequences of memory leaks.  Game programmers will also benefit as they typically need to load and unload a huge amount of resources in a game.

For the closing point, all of the C++ developers out there trying to squeeze every last bit of performance for performance intensive applications, do utilize the new features which gives C++ an edge over the other programming languages being used.


Vivek Prajapati

A moderate level programmer interested in administration and Arduino. Familiar with C++, Java, PHP, C# with my favourite being C++. Just finished my bachelor's degree in IT.

Related Articles

1 Comment

  1. Artem Razin July 3, 2014 at 2:52 pm

    Absilutely agree, This is a power of C++.

    But anyway if you work with for instance GDI resources or memory, there are some classes that manage these resources. These classes might have errors.

    I just want to say to not forget about memory profilers (like Deleaker so far), and static checkers (PC Lint etc.): especially if you write a core class that has to work with… “unmanaged”, “naked” resources.

Leave a comment

Back to Top