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:
Uses Boehm-Demers-Weiser GC Library
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.
- 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.
- 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:
Uses Reference counting algorithm
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.
- 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.
- 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