Experiencing memory issues when connecting to Exchange Online Management using C#

Vishali Gupta 0 Reputation points
2024-07-26T03:59:40.6233333+00:00

I am experiencing memory issues when connecting to Exchange Online Management using C#. The process involves executing commands such as Get-User, Get-DistributionGroupMember, Add-DistributionGroupMember, and Remove-DistributionGroupMember. After each Exchange connection, there is a memory spike that does not reduce, and with every new connection, the spike increases.

I have tried all suggested ways to connect to Exchange Online as recommended by Microsoft here: Reducing Memory Consumption of the Exchange Online PowerShell V3 module.

Docker Base Image: I use the base image mcr.microsoft.com/dotnet/sdk:8.0, which runs on a Linux container. I assumed the memory issue was caused by the OS, so I changed the Docker image to a Windows base image: mcr.microsoft.com/dotnet/sdk:8.0-windowsservercore-ltsc2022. This change reduced memory usage but did not resolve the continuous memory spikes after each connection.

Exchange Module Version:I downgraded the Exchange module from version 3.5.0 to 3.4.0, but this did not improve the situation.

Garbage Collection:I attempted to run garbage collection forcefully, but this had no effect on memory consumption.

Despite these efforts, I continue to face significant memory consumption that impacts performance. Any guidance on troubleshooting and resolving these memory issues would be greatly appreciated.

dotnet : 8.0 packages : <PackageReference Include="Microsoft.Identity.Client" Version="4.56.0" /> <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.0" /> <PackageReference Include="System.Management.Automation" Version="7.4.0" />

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,009 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,578 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,592 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Noah Ma-MSFT 2,725 Reputation points Microsoft Vendor
    2024-07-26T09:32:48.18+00:00

    Hi @Vishali Gupta,

    Welcome to the Microsoft Q&A platform!

    Please kindly understand that the Exchange Online tag here we mainly focus on general issues about Exchange Online. Looks like your issue is more related to the code.

    However, I have the followings to share with you for your issue. You can check your code and optimize it to solve the memory issue. And instead of creating a new connection for each command invocation, try to reuse the same connection session if possible. Ensure that only necessary modules are loaded into your PowerShell run space to reduce memory usage.

    Also, you could refer to documentation for the Exchange Online PowerShell V3 module About the Exchange Online PowerShell V3 module | Microsoft Learn for more information.

    If my answer is helpful to you, please mark it as the answer so that other users can refer to it. Thank you for your support and understanding.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.