How to instruct PowerShell to garbage collect .NET objects like XmlSchemaSet?

前端 未结 2 1111
温柔的废话
温柔的废话 2021-01-11 16:38

I created a PowerShell script which loops over a large number of XML Schema (.xsd) files, and for each creates a .NET XmlSchemaSet object, calls Add() and Compile() to add a

相关标签:
2条回答
  • 2021-01-11 17:01

    Microsoft has confirmed that this is a bug in PowerShell 2.0, and they state that this has been resolved in PowerShell 3.0.

    The problem is that an event handler registered using Register-ObjectEvent is not garbage collected. In reponse to a support call, Microsoft said that

    "we’re dealing with a bug in PowerShell v.2. The issue is caused actually by the fact that the .NET object instances are no longer released due to the event handlers not being released themselves. The issue is no longer reproducible with PowerShell v.3".

    The best solution, as far as I can see, is to interface between PowerShell and .NET at a different level: do the validation completely in C# code (embedded in the PowerShell script), and just pass back a list of ValidationEventArgs objects. See the fixed reproduction script at https://gist.github.com/3697081: that script is functionally correct and leaks no memory.

    (Thanks to Microsoft Support for helping me find this solution.)


    Initially Microsoft offered another workaround, which is to use $xyzzy = Register-ObjectEvent -SourceIdentifier XYZZY, and then at the end do the following:

    Unregister-Event XYZZY
    Remove-Job $xyzzy -Force
    

    However, this workaround is functionally incorrect. Any events that are still 'in flight' are lost at the time these two additional statements are executed. In my case, that means that I miss validation errors, so the output of my script is incomplete.

    0 讨论(0)
  • 2021-01-11 17:07

    After the remove-variable you can try to force GC collection :

    [GC]::Collect()
    
    0 讨论(0)
提交回复
热议问题