Dre*_*mer 5 .net c# powershell powershell-2.0 c#-3.0
我对powershell编程很新.我正在创建远程会话以编程方式执行某些命令.然而,它消耗了如此多的内存(大约150到200 MB).和更多的会话,更多的内存.
你能帮我解决一下这个问题吗?
观察:1.执行CreateRunSpace命令后,其消耗约3MB.
有人在创建运行空间时面临同样的问题:
"可能的PowerShell Runspace处理泄漏"
删除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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行空间对象是一次性的,如果您编写代码是否相同:
using (Runspace rs = RunspaceFactory.CreateRunspace())
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1920 次 |
| 最近记录: |