New-Object是否存在内存泄漏? [英] Does New-Object have a memory leak?
问题描述
使用Powershell v3,我使用.net库类System.DirectoryServices.DirectoryEntry
和System.DirectoryServices.DirectorySearcher
从域中的用户查询属性列表.基本上,此代码为在这里找到.
Using Powershell v3, I'm using the .net library class System.DirectoryServices.DirectoryEntry
and System.DirectoryServices.DirectorySearcher
to query a list of properties from users in a domain. The code for this is basically found here.
您唯一需要添加的是在$ds.Filter = '(&(objectCategory=person)(objectClass=user))'
和$ds.PropertiesToLoad.AddRange($properties)
行之间的第$ds.PageSize=1000
行.这将消除仅吸引1000个用户的限制.
The only thing you need to add, is a line $ds.PageSize=1000
in between lines $ds.Filter = '(&(objectCategory=person)(objectClass=user))'
and $ds.PropertiesToLoad.AddRange($properties)
. This will remove the limit of only grabbing 1000 users.
我在一个域(我们称为domain1
)中的用户数量已超过80,000.另一个域(我们称为domain2
)拥有超过200,000个用户.
The number of users from one domain (we'll call it domain1
) I have has over 80,000. Another domain (we'll call this domain2
) has over 200,000 users.
如果我在domain1
上运行代码,大约需要12分钟(与Get-QADUser
花费的24小时相比,这真是太棒了).但是,在PowerShell窗口中完成脚本后,我注意到还剩下500mb的内存. Domain2
留下大约1.5gb
的记忆.
If I run the code on domain1
, it takes roughly 12 minutes (which is fantastic compared to the 24 hours Get-QADUser
was taking). However, after the script is finished in the PowerShell window, I notice a memory hog is left of about 500mb. Domain2
leaves a memory hog of about 1.5gb
.
注意:Get-QADUser
的内存泄漏要严重得多,更糟. Domain2
留下大约6gb的内存,大约需要72个小时才能完成(相对于.net类,该时间还不到一个小时).
Note: the memory leak with Get-QADUser
is much much MUCH worse. Domain2
leaves a memory hog of about 6gb and takes roughly 72 hours to complete (vs less than an hour with the .net class).
释放内存的唯一方法是关闭PowerShell窗口.但是,如果我想编写一个脚本来调用所有这些域脚本以一个接一个地运行它们,该怎么办?进入第6个脚本后,我的内存将耗尽.
The only way to free the memory is to close the PowerShell window. But what if I want to write a script to invoke all these domains scripts to run them one after the other? I would run out of memory after getting to the 6th script.
我唯一能想到的是New-Object
正在创建一个构造函数,并且没有析构函数(与java不同).我曾尝试在循环迭代期间使用[System.GC]::Collect()
,但这没有任何影响.
The only thing I can think of is New-Object
is creating a constructor and does not have a destructor (unlike java). I've tried using the [System.GC]::Collect()
during the loop iterations, but this has had no affect.
有原因吗?它可以解决吗?还是我坚持呢?
Is there a reason? Is it solvable or am I stuck with this?
推荐答案
要注意的一件事:如果确实存在内存泄漏,则可以在新的shell中运行脚本:
One thing to note: If there actually is a memory leak, you can just run the script in a new shell:
powershell { <# your code here #> }
对于泄漏,只要您有任何引用保留大数据的对象的变量,就无法收集该对象.通过使用内存分析器查看内存中仍然存在的内容以及原因,可能会很幸运.据我所知,如果您在脚本文件中使用该代码并执行脚本(使用&
,而不使用.
!),那么这实际上就不会发生.
As for the leak, as long as you have any variables that reference an object that holds on to large data it cannot be collected. You may have luck by using a memory profiler to look at what is still in memory and why. As far as I can see, if you use that code in a script file and execute the script (with &
, not with .
!), then this shouldn't really happen, though.
这篇关于New-Object是否存在内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!