Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why garbage collection when RAII is available?

I hear talks of C++14 introducing a garbage collector in the C++ standard library itself. What is the rationale behind this feature? Isn't this the reason that RAII exists in C++?

  • How will the presence of standard library garbage collector affect the RAII semantic?
  • How does it matter to me(the programmer) or the way in which I write C++ programs?
like image 236
Alok Save Avatar asked Jun 23 '13 14:06

Alok Save


People also ask

Is Raii garbage collection?

RAII and GC solve problems in completely different directions. They are completely different, despite what some would say. Both address the issue that managing resources is hard. Garbage Collection solves it by making it so that the developer doesn't need to pay as much attention to managing those resources.

What is the reason behind garbage collector?

The garbage collector provides the following benefits: Frees developers from having to manually release memory. Allocates objects on the managed heap efficiently. Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations.

Does garbage collection happen automatically?

In Java, garbage collection happens automatically during the lifetime of a program. This eliminates the need to de-allocate memory and therefore avoids memory leaks. Java Garbage Collection is the process by which Java programs perform automatic memory management.

When should you not use garbage collection?

Show activity on this post. The obvious cases for not using garbage collection are hard realtime, severely limited memory, and wanting to do bit twiddling with pointers.


2 Answers

Garbage collection and RAII are useful in different contexts. The presence of GC should not affect your use of RAII. Since RAII is well-known, I give two examples where GC is handy.


Garbage collection would be a great help in implementing lock-free data structures.

[...] it turns out that deterministic memory freeing is quite a fundamental problem in lock-free data structures. (from Lock-Free Data Structures By Andrei Alexandrescu)

Basically the problem is that you have to make sure you are not deallocating the memory while a thread is reading it. That's where GC becomes handy: It can look at the threads and only do the deallocation when it is safe. Please read the article for details.

Just to be clear here: it doesn't mean that the WHOLE WORLD should be garbage collected as in Java; only the relevant data should be garbage collected accurately.


In one of his presentations, Bjarne Stroustrup also gave a good, valid example where GC becomes handy. Imagine an application written in C/C++, 10M SLOC in size. The application works reasonably well (fairly bug free) but it leaks. You neither have the resources (man hours) nor the functional knowledge to fix this. The source code is a somewhat messy legacy code. What do you do? I agree that it is perhaps the easiest and cheapest way to sweep the problem under the rug with GC.


As it has been pointed out by sasha.sochka, the garbage collector will be optional.

My personal concern is that people would start using GC like it is used in Java and would write sloppy code and garbage collect everything. (I have the impression that shared_ptr has already become the default 'go to' even in cases where unique_ptr or, hell, stack allocation would do it.)

like image 89
Ali Avatar answered Oct 22 '22 07:10

Ali


I agree with @DeadMG that there is no GC in current C++ standard but I would like to add the following citation from B. Stroustrup:

When (not if) automatic garbage collection becomes part of C++, it will be optional

So Bjarne is sure that it will be added in future. At least the chairman of the EWG (Evolution Working Group) and one of the most important committee members (and more importantly language creator) wants to add it.

Unless he changed his opinion we can expect it to be added and implemented in the future.

like image 35
sasha.sochka Avatar answered Oct 22 '22 08:10

sasha.sochka