相关疑难解决方法(0)

如何指示PowerShell垃圾收集.NET对象,如XmlSchemaSet?

我创建了一个PowerShell脚本,它循环遍历大量的XML Schema(.xsd)文件,并为每个文件创建一个.NET XmlSchemaSet对象,调用Add()并向Compile()其添加模式,并打印出所有验证错误.

此脚本可以正常工作,但某处存在内存泄漏,如果在100个文件上运行,则会占用数十亿字节的内存.

我在循环中基本上做的是以下内容:

$schemaSet = new-object -typename System.Xml.Schema.XmlSchemaSet
register-objectevent $schemaSet ValidationEventHandler -Action {
    ...write-host the event details...
}
$reader = [System.Xml.XmlReader]::Create($schemaFileName)
[void] $schemaSet.Add($null_for_dotnet_string, $reader)
$reader.Close()
$schemaSet.Compile()
Run Code Online (Sandbox Code Playgroud)

(可以在此要点中找到重现此问题的完整脚本:https://gist.github.com/3002649.只需运行它,并在任务管理器或Process Explorer中观察内存使用量的增加.)

受到一些博客帖子的启发,我尝试添加

remove-variable reader, schemaSet
Run Code Online (Sandbox Code Playgroud)

我也尝试过$schema从中Add()做起

[void] $schemaSet.RemoveRecursive($schema)
Run Code Online (Sandbox Code Playgroud)

这似乎有一些影响,但仍有泄漏.我假设旧的实例XmlSchemaSet仍在使用内存而不进行垃圾回收.

问题:我如何正确地教垃圾收集器它可以回收上面代码中使用的所有内存?或者更一般地说:如何通过有限的内存来实现我的目标?

powershell xsd garbage-collection memory-leaks xmlschemaset

11
推荐指数
1
解决办法
2万
查看次数

如何从PowerShell调用显式实现的接口方法?

码:

add-type @"
    public interface IFoo
    {
        void Foo();
    }

    public class Bar : IFoo
    {
        void IFoo.Foo()
        {
        }
    }
"@ -Language Csharp

$bar = New-Object Bar
($bar -as [IFoo]).Foo() # ERROR.
Run Code Online (Sandbox Code Playgroud)

错误:

方法调用失败,因为[Bar]不包含名为'Foo'的方法.

powershell casting explicit-interface

7
推荐指数
3
解决办法
3824
查看次数