Introduction To Garbage Collection in Dart

mobile academy

In the previous post we learned about Dart VM, Garbage collection is one of the feature provided by Dart VM. Understanding garbage collector is crucial when you want to build a efficient applications. It allows you to manage memory resources effectively, preventing memory leaks and improving the overall performance and stability of your programs.

What Is Garbage Collection?

The Dart VM uses a form of automatic memory management where the garbage collector attempts to reclaim memory occupied by objects that are no longer in use by the program. Garbage collection primarily focuses is to managing dynamically allocated memory on the heap.

How is the memory allocated to heap?

In Dart, memory allocation on the heap typically occurs when you create objects using the new keyword or object constructors.

What are the steps taken by Dart Garbage Collector to free up memory?

  1. Marking:
    1. The garbage collector thoroughly scans the heap, which is the memory region where objects reside.
    2. It identifies which objects are still actively referenced by your program and marks them as “in use.”
  2. Sweeping:
    1. Once objects are marked, the garbage collector pinpoints the objects that are no longer referenced and therefore eligible for removal.
    2. These unreferenced objects are swept away, freeing up valuable memory space.
  3. Generational Approach:
    1. The Dart VM employs a generational garbage collection strategy:
      1. New objects are allocated in the new generation, which is frequently garbage collected to keep it clean and efficient.
      2. Objects that survive multiple garbage collections are promoted to the old generation, which is collected less often, reducing the overall overhead of garbage collection.
  4. Optional Compaction:
    1. Although not always performed, the garbage collector can optionally perform a compaction step.
    2. This involves rearranging the remaining objects in memory to create larger, contiguous blocks of free space.
    3. Compaction can help mitigate memory fragmentation, which can negatively impact performance over time.
  5. Continuous Process:
    1. Garbage collection is not a one-time event. It’s a continuous process that runs throughout your program’s execution to ensure efficient memory usage and optimal performance.

What is a generational approach used by Dart VM garbage collector?

The generational approach is a key strategy employed by the Dart VM’s garbage collector to optimize memory usage and improve performance. It works by dividing the heap (the memory area where objects reside) into two generations:

  1. New Generation: This generation holds recently allocated objects. It’s frequently scanned for garbage collection (typically mark-and-sweep), removing unreferenced objects swiftly. This helps prevent memory fragmentation and keeps the new generation small and efficient.
  2. Old Generation: Objects that survive multiple garbage collections in the new generation are promoted to the old generation. This generation is scanned less frequently than the new one, reducing overall garbage collection overhead. However, objects in the old generation can persist longer, potentially leading to fragmentation if they die at uneven intervals.

Memory fragmentation is when the sum of the available space in a memory heap is large enough to satisfy a memory allocation request but the size of any individual fragment (or contiguous fragments) is too small to satisfy that memory allocation request.

What are the benefits of using generational approach in Dart VM?

  • Reduced Memory Fragmentation: Frequent collection in the new generation minimizes fragmentation, meaning smaller wasted space between active objects. This allows smoother allocation of new objects without needing to search for fragmented pockets.
  • Improved Performance: Less frequent collection in the old generation reduces the overall overhead of garbage collection, boosting the program’s performance.
  • Balance Between Efficiency and Overhead: The approach balances efficient memory usage in the new generation with lower overhead in the old generation, aiming for an optimal overall performance.

However, it’s important to note that:

  • Full GC pauses can still occur: While less frequent, garbage collection in the old generation can still cause brief pauses in program execution, especially for long-lived applications with large object populations.
  • Generational approach does not eliminate fragmentation entirely: While significantly reduced, fragmentation can still occur in the old generation if object lifetimes vary greatly.

Become a Job Ready Flutter developer
Select your currency
USD United States (US) dollar