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

Robotic Process Automation Singapore

CFB Bots is a leading technology service provider in the fast-growing field of Intelligent Automation. We partner with large enterprises in their Digital Transformation journey and help them and their employees thrive in the Future of Work.

NAVIGATE
CONTACT US
STAY CONNECTED

Singapore

CFB Bots Pte Ltd

Registration No.: 201705263H

77 High Street

#05-09, High Street Plaza

Singapore 179433

​

T: +65 6909 2099

E: enquiries@cfb-bots.com

​​

Malaysia

CFB Bots Sdn Bhd

Registration No.: 202401045826 (1591672-X)

Suite: 33-01, 33rd Floor

Menara Keck Seng

203 Jalan Bukit Bintang

55100 Kuala Lumpur

​​

T: +60 39 388 0352

E: my@cfb-bots.com

​

Australia

T: +61 2 8880 5998

E: au@cfb-bots.com

Join our mailing list to get the latest insights on automation

  • LinkedIn Social Icon
  • Facebook Social Icon
  • Twitter Social Icon
  • Instagram Social Icon
  • YouTube Social  Icon
TS Master Logo.png

© 2017-2025 CFB Bots Pte Ltd. All Rights Reserved. Tel: (65) 6909 2099 | Email: enquiries@cfb-bots.com | Privacy Policy | Terms of Use

bottom of page