VMware Cloud Community
Selvalakshmi
Contributor
Contributor

Encountering 'System.OutOfMemoryException' when Reading VMs from ESXi Hypervisor

I'm encountering an issue while trying to read VMs from an ESXi hypervisor using the following C# function

 

public static ReadHypervisorVmListRspn ReadHypervisorVmList(ReadHypervisorVmListRqst rqst)
{
PowerShellClient ps = null;
try
{
ps = new PowerShellClient();
var rspn0 = ps.ExecuteScript("Set-ExecutionPolicy -Scope CurrentUser Unrestricted");
var rspn = new PowerShellClient.ExecuteRspn();
//login
var cmd = "Connect-VIServer -Server " + rqst.LoginInfo.HostUrl + " -Protocol https -User " + rqst.LoginInfo.HostUserName +
" -Password " + rqst.LoginInfo.HostPassword;
rspn = ps.ExecuteScript(cmd);
if (rspn.Response != "OK")
{
return new ReadHypervisorVmListRspn { Response = "Unable to connect !!" };
}

//action
cmd = "";
cmd = "Get-Vm | Select-Object Name,PowerState,NumCPU,MemoryGB,UsedSpaceGB,ProvisionedSpaceGB,@{N='GuestFullName';E={@($_.guest.OSFullName)}},@{N='PrivateIp';E={@($_ | Get-View).Guest.Ipaddress}},@{N='MacAddress';E={($_ | Get-NetworkAdapter).MacAddress}},@{N='Uuid'; E={($_ |Get-View).config.uuid}},@{N='UpTime';E={($_ |Get-View).Summary.QuickStats.Uptimeseconds}},@{n='HardDiskSizeGB'; e={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}}";
var rspn1 = ps.ExecuteScript(cmd, true, 1);
if (rspn1.Response.Contains("An item with the same key has already"))
rspn1 = ps.ExecuteScript(cmd);
if (rspn1.Response != "OK")
{
//logout
cmd = "Disconnect-VIServer -Confirm:$false;";
var rspn5 = ps.ExecuteScript(cmd);
return new ReadHypervisorVmListRspn { Response = rspn1.Response };
}
var rspn3 = rspn1.Output;

//logout
cmd = "Disconnect-VIServer -Confirm:$false;";
var rspn4 = ps.ExecuteScript(cmd);

var VmInfo = Json4.GetType<List<VmInfo>>(rspn3);
var rspn2 = new ReadHypervisorVmListRspn()
{
Response = "OK",
VmInfo = VmInfo
};

return rspn2;
}
catch (Exception ex)
{
return new ReadHypervisorVmListRspn { Response = "Error" };
}
finally
{
ps = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

 

The function sometimes works, but most of the time I encounter the 'System.OutOfMemoryException' error. It returns errors like:

18-02-2024 16:14:19 Get-NetworkAdapter Exception of type 'System.OutOfMemoryException' was thrown. 18-02-2024 16:14:20 Get-NetworkAdapter Exception of type 'System.OutOfMemoryException' was thrown. 18-02-2024 16:14:20 Get-HardDisk Exception of type 'System.OutOfMemoryException' was thrown. 18-02-2024 16:14:21 Get-View Exception of type 'System.OutOfMemoryException' was thrown.

I've also tried fetching VMs in batches using a loop, but the issue persists. Is there any possible way to fix this issue?

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership

Which PowerCLI version?
Is this a 32- or 64-bit app?
Which type of memory is it? Is it heap storage?
Did you already use perfmon to check what exactly is happening?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Selvalakshmi
Contributor
Contributor

I used VMware.PowerCLI version 13.1.0.21624340, and my C# app is 64-bit, utilizing heap storage. During the exception, the Performance Monitor reported memory usage as : Used Memory = 8 GB, Available Memory = 22 GB.

Reply
0 Kudos
LucD
Leadership
Leadership

You might want to perform a more thorough analysis than just looking at the Performance Monitor.
This .NET Memory Performance Analysis document is helpful.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos