As a developer, we need not to explicitly configure memory allocation and deallocation in the system. This process triggers automatically, when the object is created memory is automatically allocated to it and when it is terminated the memory is destroyed by the background process called Garbage Collector to free up the space for new objects.
The objects using dynamic memory allocation captures some memory and that memory is allocated until there are references to that object. When there is no reference to the object, it is assumed that the object is no longer need. Garbage Collector identifies the unused objects (no more references to the object are there) and scrap it out for other objects to reuse the space. It is called periodically by ABAP runtime environment.
The programs that do not de-allocate memory at last collapse when there is no more memory left in the system to allocate. This scenario is termed as a memory leak in the program. As garbage collector is an automated process, the number of memory leak errors is minimised.
Why proper Garbage Collection is important?
Garbage collection plays an important role in system programming. The unseemly behaviour of the garbage collector can result in critical problems, like:
- Increased memory requirement
- Performance degradation
Garbage collection eventually reduces or eliminates some bugs:
- Dangling pointer bugs: When there are one or more references to the object is used in the program but the memory is freed.
- Double free bugs: To free the memory which is already freed.
- Memory leaks: When memory is not cleared and objects have no reference, this will lead to memory exhaustion.
Garbage collectors can sometimes bring runtime overhead that can serve as the performance problem for a larger application. Hence, it is critical to implement the right garbage collection method to optimize the application performance.