C#System.Management.Automation.Powershell类在Invoke()调用上泄漏内存 [英] C# System.Management.Automation.Powershell class leaking memory on Invoke() call

查看:199
本文介绍了C#System.Management.Automation.Powershell类在Invoke()调用上泄漏内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#测试应用程序的代码,可以使用PowerShell查询系统信息,并且其中有一些简单的功能,如下所示:

public void GetSystemInfo()
{
    using (PowerShell ps = PowerShell.Create())
    {
        ps.AddCommand("Get-Disk");      // Get-Disk is an example; this varies
        foreach (PSObject result in ps.Invoke())
        {
            // ...
            // Do work here. Process results, etc...
            // ...
        }
    }
}

这很简单,主要取自MSDN示例,例如: https://msdn.microsoft.com/zh-我们/library/dd182449(v=vs.85).aspx

问题在于,每次调用此函数时,我都可以看到该应用程序的内存占用量在增加.占用空间在调用ps.Invoke()时就增加了,并且从未缩小. 我如何处理数据都没有关系;我什至可以完全注释掉foreach循环的主体,并且永远不会垃圾收集内存.

据我从查看PowerShell类

再次,超级简单.摘自示例此处.

我想知道这是否是Powershell模块中的已知错误.也许我会开始寻找Powershell界面(WMI等)的替代方法,因为我需要在不确定的长时间内定期调用这些PS查询.

解决方案

C#库中的Powershell API确实确实存在问题.即使强行进行垃圾回收也似乎无法释放内存.

使用WMI调用代替Powershell替换所有内容,从而消除了此问题.

I have code for a C# test app that queries system info using PowerShell, and in it I have a few simple functions like the following:

public void GetSystemInfo()
{
    using (PowerShell ps = PowerShell.Create())
    {
        ps.AddCommand("Get-Disk");      // Get-Disk is an example; this varies
        foreach (PSObject result in ps.Invoke())
        {
            // ...
            // Do work here. Process results, etc...
            // ...
        }
    }
}

This is straightforward and mostly taken from MSDN examples, like here: https://msdn.microsoft.com/en-us/library/dd182449(v=vs.85).aspx

The problem is that each time this function is called, I can see the app's memory footprint grow. The footprint grows right at the call to ps.Invoke() and never shrinks. It doesn't matter how I process the data; I can even comment the body of the foreach loop out completely and the memory is never garbage collected.

As far as I can tell from looking at the PowerShell class interface, there doesn't seem to be a way to force it to clean itself up, and Dispose() clearly doesn't work since the resources remain leaking in memory after the using block exits.

Is this a known bug in the C# PowerShell class implementation or PowerShell itself? Is anyone aware of a solution, or an alternative if possible?


EDIT

I've also tried the follow now, with the same results:

public void GetSystemInfo()
{
    Runspace rs = RunspaceFactory.CreateRunspace();
    rs.Open();

    PowerShell ps = PowerShell.Create();
    ps.Runspace = rs;
    ps.AddCommand("Get-Disk");      // Get-Disk is an example; this varies
    foreach (PSObject result in ps.Invoke())
    {
        // ...
        // Do work here. Process results, etc...
        // ...
    }

    rs.Close();
}

Again, super straightforward. Taken from the sample here.

I'm wondering if this is a know bug in the Powershell module. Perhaps I'll start looking into alternatives to the Powershell interface (WMI, etc) since I need to call these PS queries periodically over an indeterminately long duration.

解决方案

There does indeed appear to be an issue with the Powershell API in the C# library. Even forcing a garbage collect doesn't seem to free up the memory.

Replacing everything with WMI calls instead of Powershell eliminates the problem.

这篇关于C#System.Management.Automation.Powershell类在Invoke()调用上泄漏内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆