【发布时间】:2011-11-11 06:00:13
【问题描述】:
我对 powershell 编程相当陌生。我正在创建远程会话以编程方式执行一些命令。但是它消耗了很多内存(大约 150 到 200 MB)。并且更多的会话,它的更多内存。
你能帮我解决这个问题吗?
- 罪魁祸首是什么?
- 我该如何解决?
观察: 1. 执行 CreateRunSpace 命令后,消耗约 3MB。
有人在创建运行空间时遇到了同样的问题:
“可能的 PowerShell 运行空间句柄泄漏”
仍在调查以找到答案...
删除 pssession 使运行空间释放句柄并修复内存泄漏。它现在在 ~30 到 40MB 之间切换。
谢谢!
(仅供参考 - 引用 System.Management.Automation.dll)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation.Runspaces;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int success = 0;
int fails = 0;
for (int i = 0; i < 50; i++)
{
Runspace rs = RunspaceFactory.CreateRunspace();
//After this the memory will be incremented by ~3Mb in taskmanager
rs.Open();
using (Pipeline pl = rs.CreatePipeline())
{
Command cmd = new Command("new-pssession");
pl.Commands.Add(cmd);
var retval = pl.Invoke();
if (retval.Count > 0)
{
success++;
}
else
{
fails++;
}
pl.Stop();
}
rs.Close();
rs = null;
}
GC.Collect();
}
}
}
【问题讨论】:
-
你能澄清一下“~15o 到 200”是什么意思吗?你的意思是“~150MB 到 200MB”吗?
-
它的 MB。谢谢你。更新相同。
-
您可能在某处发生了内存泄漏。我建议研究一些分析工具,例如 ANTS Memory Profiler 和 ANTS Performance Profiler。
-
有什么解决办法吗?任何示例完整源代码?
标签: c# .net powershell c#-3.0 powershell-2.0