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.