Skip to main content

Out of Memory issues with C# Extend Apps

Last updated on October 24, 2024

The .NET C# Extend app uses a garbage collector to automatically manage system memory. However, memory issues may still occur due to:

  • Holding references onto large objects for far longer than necessary
  • Managed memory leaks
  • Unmanaged memory leaks
  • General high memory usage

This article contains best practices and a list of recommended tools to use for efficient memory management and mitigate out of memory (OOM) issues in the local development of your .NET C# Extend app.

Best practices

  1. Use appropriate data type. Using Span<T> wherever possible to reduce the memory usage.

  2. Avoid string concatenation since string is immutable. Use StringBuilder instead.

  3. Limit the amount of data retrieved from other source.

  4. Avoid using unmanaged code. The garbage collector in the .NET C# Extend app does not clean up native resources. Improper implementation of unmanaged code may lead to high memory usage.

To manage your CPU and memory usage, we recommend using the following tools:

Visual Studio

Visual Studio provides great tools to analyze and diagnose memory issues in .NET application.

Visual Studio diagnostic tools

  1. Debug the .NET C# application using Visual Studio.
  2. On the right sidebar, select Diagnostic Tools and pin it. The total process memory will be shown live.
  3. Go to Memory Usage to take a snapshot and analyze memory usage of your app.
  4. Go to CPU Usage to view CPU profiling info.
  5. Click "Enable CPU profiling to see a breakdown of CPU usage by function."
  6. Put a breakpoint or use break all to see CPU profile.

For more information, refer to Visual Studio's official documentation.

Alternatively, you can use Jetbrains Dotmemory to do CPU and memory profiling for .NET applications. For more information, refer to the official Jetbrains documentation.

Dotnet CLI

Dotnet tools allow you to debug memory leaks on Linux and trace high CPU usage.