Orders from 10/104/24 onwards ship on 12/04/2024

Free Shipping for orders over ₹1999

support@thinkrobotics.com | +91 93183 94903

What is Garbage Collection?

What is Garbage Collection? | Difference Between Garbage Collection in C & C++

Garbage Collection (GC) is a mechanism that allows for the automatic reclamation of unused memory blocks. Programmers allocate memory dynamically, but when a block is no longer required, they do not have to explicitly return it to the system with a free() call. The GC engine is responsible for recognizing when a certain block of allocated memory (heap) is no longer in use and returning it to the free memory area. John McCarthy created GC in 1958 as the LISP language's memory management technique. GC techniques have advanced since then and can now compete with explicit memory management. GC is the foundation of several languages. The most popular is certainly Java, although others include LISP, Scheme, Smalltalk, Perl, and Python. The most significant exceptions to this list are C and C++, which follow in the tradition of a respectable, low-level approach to system resource management.

Few languages require garbage collectors to be included as part of the language in order to be efficient. These are known as garbage-collected languages. For example, garbage collection is required for the operation of Java, C#, and the majority of scripting languages. Languages such as C and C++, on the other hand, offer manual memory management, which operates similarly to the garbage collector. Few languages enable both garbage collection and manually managed memory allocation/deallocation, in which case a separate heap of memory is allocated to the garbage collector and manual memory.

 

Difference Between Garbage Collection in C & C++

C Programming Language: 

Implementation: 

Uses Boehm-Demers-Weiser GC Library

Working:

Using the library is a rather simple operation; for newly generated programmes, you simply execute GC alloc() to obtain memory and then forget about it when you no longer require it. Set all the pointers that refer to it to NULL. For existing sources, replace all allocation calls (malloc, calloc, realloc) with GC-enabled ones. All free() calls are replaced with nothing, but all relevant pointers are changed to NULL.

Pros:

  • GC is an efficient means of dealing with memory-related issues in C, relieving the programmer of memory accounting responsibilities.
  • When a memory block is in use, the GC engine knows, and the block immediately disappears when it is no longer referenced. GC prevents all premature frees and memory leaks from occurring in the first place.

Cons:

  • Being unable to predict how long an allocation will take (allocation latency and jitter), which is frequently a concern for real-time systems.
  • Longer execution time as a result of GC processing overhead.

 

C++ Programming Language:

Implementation: 

Uses Reference counting algorithm

Working:

A reference count will be assigned to each dynamic memory. When a reference is made, the reference count rises, and when a reference is destroyed, the reference count falls. When the reference count reaches 0, it indicates that the memory is no longer in use and can be freed.

Pros:

  • Automatic deallocation relieves a programmer of the responsibility of memory management, improving system readability, and reducing development time and expenses.
  • Explicit memory management presents the possibility of memory management mistakes, such as memory leaks. As a result, explicit deallocation reduces reliability.

Cons:

  • The time or CPU cycles required to locate and delete unused memory, even if the user is aware of which pointer memory can be released and is not in use.
  • We won't know when it's deleted or when the destructor is called.

 Check out more of our blogs/articles and products at thinkrobotics.in

References: 

  • https://www.linuxjournal.com/article/6679
  • https://rebelsky.cs.grinnell.edu/Courses/CS302/99S/Presentations/GC/
  • https://www.educba.com/c-plus-plus-garbage-collection/
  • https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)