Microsoft365DSC icon indicating copy to clipboard operation
Microsoft365DSC copied to clipboard

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

Open vishali-gupta-robomq opened this issue 1 year ago • 2 comments

Description of the issue

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.

  1. 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.

  2. 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.

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

  4. 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" />

Environment Information + PowerShell Version

No response

vishali-gupta-robomq avatar Jul 25 '24 16:07 vishali-gupta-robomq

could this be why we have the memory issues for the Exchange module on the more recent DSC versions?

tayhall avatar Jul 26 '24 14:07 tayhall

This issue occurs with Exchange Online versions 3.4.0 and 3.5.0 using C#, but I have not tried it with earlier versions. Every time I create an Exchange connection, it causes a memory spike. I use the same Exchange connection for 1 hour and 15 minutes, and after this period, when I create a new connection, I observe a spike. When I create a new connection, I destroy all the previous connections using Disconnect-ExchangeOnline and dispose of PowerShell runspace and session, but the issue still persists. The problem is that the memory spike does not go down even after these steps.

Additionally, during the debugging phase of my C# application, I observed that some string objects hold memory and are filled with every new connection, but do not release resources. I have disposed of all PowerShell sessions and runspaces, but these objects are still not getting released.

vishali-gupta-robomq avatar Jul 26 '24 17:07 vishali-gupta-robomq

@vishali-gupta-robomq Any chance you can persist the session with the authentication context and share it in the other processes somehow? That's we just did with the latest version of MSCloudLoginAssistant. If not, then there isn't really anything we can do and the product group has to fix it.

FabienTschanz avatar Dec 13 '24 14:12 FabienTschanz

@vishali-gupta-robomq Any updates you can share? Is the issue still present or did you find anything to mitigate it?

FabienTschanz avatar Feb 20 '25 07:02 FabienTschanz

Closing for now. The ExchangeOnline module got improvements in memory handling in version 3.7.1, but it doesn't directly target Microsoft365DSC.

FabienTschanz avatar Mar 05 '25 21:03 FabienTschanz