top of page

How to Handle Memory Leaks Using Garbage Collection

💡 Memory leaks can pose significant challenges to bot performance and stability. This guide explains how to use garbage collection effectively to manage and reclaim memory, ensuring efficient resource utilization and robust bot behavior.

Problem Description

Memory leaks occur when the automation process continuously allocates memory without releasing it, eventually leading to degraded performance or crashes. You might be experiencing memory leak if your process encountering the error message "Job Stopped with Unexpected Exit Code" randomly.


Garbage collection automates memory management by identifying and reclaiming unused objects. However, improper usage or over-reliance on manual garbage collection can lead to performance bottlenecks. Understanding and implementing garbage collection effectively is key to mitigating memory leaks and optimizing system resources.

Solution

Step 1: Create a New Workflow

  1. Create a new Sequence workflow and renamed it as ‘ReusableHandleMemoryLeak.xaml’.


Step 2: Add an "Invoke Code" Activity

  1. Drag and drop an Invoke Code activity into your workflow.

  2. Set the Language property to VB.Net.

  3. Enter the following code snippet:

System.GC.Collect()
System.GC.WaitForPendingFinalizers()
System.GC.Collect()

Explanation of the Code

  • System.GC.Collect(): Forces the garbage collector to run immediately, attempting to free memory by reclaiming unused objects.

  • System.GC.WaitForPendingFinalizers(): Waits until all objects requiring finalization (e.g., objects with cleanup logic) are processed, ensuring proper resource release (e.g., file handles or database connections).

  • System.GC.Collect(): A final call to ensure all pending objects are cleared and memory is fully reclaimed.



Step 3: Test and Validate

  1. Integrate the workflow into your application to monitor its effectiveness in handling memory leaks.

  2. Run tests under various conditions to confirm that memory is being efficiently reclaimed and performance is stable. In the example below, the ReusableHandleMemoryLeak workflow is invoked before and after the digitization of the document.



Additional Information

  • Last updated on: 2 Jan 2025

  • Tested version(s): Studio 22.10.4.0

  • Prerequisites: None

  • Dependencies: None

  • Known issues: None

bottom of page