Memory management is one of the integral and crucial topics in the field of software development. It is a task of allocating memory dynamically to programs at their requests and then releasing them for reuse when they are no longer required. It plays a vital role in the smooth functioning of any software application.
Memory management is mainly focused on the allocation of the object on demand and its de-allocation after the usage. The main question arises as to when the allocated object should get released and how and when the particular program gets to know that the object is no longer required. Keeping a track of this and efficiently disposing off from the objects that are no longer needed, is a crucial task. Initially, the developers, themselves, had to keep a track of each and every object that they created, and also, ensure to carefully release/dispose them after the completion of its usage. But now the object’s life cycle is managed automatically by runtime and in every language. To attain this, we have two memory management algorithms available namely Garbage collection (GC) and Automatic Reference counting (RC).
Both algorithms keep track of how long an object will be accessible in memory before it gets destroyed and when can its memory get released to be used by other objects. Here, both approaches vary from their algorithm to implement the memory recycling process.
Garbage Collection manages its task perfectly by keeping track of the object’s life cycle by creating an object graph with the help of the application roots and heap memory. This graph decides about thereclaimable memory and retainable objects. GC process is managed by Common Language Runtime (for .Net) and by Java Runtime Environment (for Android).
Object graph in GC algorithm. Source:C# Corner
So, When does the Garbage Collection process take place? – Garbage Collection process takes place in the background thread in the following two ways:
Fixed time intervals
GC process invents object graph and destroys the non-reachable objects in every fixed time interval.
Shortage in memory
If there is a memory shortage while demanding memory for an object, the GC process starts and reclaims the memory for non-reachable objects and allocates the memory for the demanding object.
Advantages of Garbage Collection
- GC can overcome the retain cycles’ memory leakage.
Disadvantages of Garbage Collection
- Since GC happens in the background, the exact time frame for the object’s release is not determined.
- When a GC happens, sometimes other threads in the application may be temporary put on hold.
Automatic Reference Counting
In ARC, the object’s life cycle is managed by keeping track of all the valid references with an internal retain count. This count is incremented for each new copy reference, and is decremented if a reference is overwritten, or if the object that is being referred to, is released. When there is no scope for any reference and the retain count reaches zero, the object and its underlying memory is automatically freed. Complier implicitly increments/decrements the reference counts of object by injecting retain/release keyword in place, wherever applicable. Previously, the developer had to code the retain/release keyword in order to manage the reference counts. ARC shifts the burden from the developer’s shoulders and manages the reference count of the object.
When does the memory get recycled in ARC?
In ARC, there is no time frame for clearing the memory. If any object reaches its reference count to 0, the memory is recycled in ARC automatically.
Advantages of Automatic Reference Counting
- Real-time, automatic recycling of memory when the object’ reference count becomes zero.
- No background processing, and more efficient on lower-power systems, such as mobile devices.
Disadvantages of Automatic Reference Counting
- Cannot handle retain cycles.
- Retain cycle takes place when two (or more) objects refer each other, keeping each other alive even after all external references to the objects have got exhausted. Both the objects reference count will become 1.ARC does not recycle the memory even though both objects are not used anywhere at all. To handle this problem, proper storage modifiers (strong/weak) is used during the object allocation in reference counting algorithm.
Tech Lead, RapidValue Solutions