none
PowerShell scripting question - proper disposal of objects

    问题

  • Hello all,

    I wrote a script the counts the number of items in every list throughout our entire SharePoint farm.  It works great but since our farm is pretty large, the script consumes about 2GB of RAM on the server it's executed on.  I looked into disposing the different variables I'm using and some objects have a dispose method (such as spsite and spweb objects).  However, my spwebapplication and splist objects don't seem to have a dispose method.  I then tried simply placing start-spassignment -global at the beginning of my script and stop-spassignment -global at the end and although this definitely helps, the powershell environment is still consuming roughly 600MB more than it was prior to running the script.  

    In short, how does one properly dispose of objects that do not have a dispose method?  Specifically, spwebapplication and splist objects.

    Thanks,

    Tim

    2012年6月20日 14:03

答案

  • You could wrap your code in "using" statement which should allow the garbage collector to clean them up once you're done with them.  For this kind of job I prefer to write Object Model code in C# rather than scripting it in Powershell.  If that is not an option, you can use C# style code in Powershell to encapsulate much of your work and get some cleanup that way.

    It would be an interesting test to see if converting your code to C# would run better and more efficient than in Powershell.  I bet it will.  If you don't have the means to do such a convertion to OM code, feel free to email me at (c @ sharepointmvp . net) with the Powershell script and I can convert it for you and provide you with the test code.


    I trust that answers your question...

    Thanks
    C

    http://www.cjvandyk.com/blog | LinkedIn | Facebook | Twitter | Quix Utilities for SharePoint | Codeplex

    2012年6月22日 14:22

全部回复

  • You could wrap your code in "using" statement which should allow the garbage collector to clean them up once you're done with them.  For this kind of job I prefer to write Object Model code in C# rather than scripting it in Powershell.  If that is not an option, you can use C# style code in Powershell to encapsulate much of your work and get some cleanup that way.

    It would be an interesting test to see if converting your code to C# would run better and more efficient than in Powershell.  I bet it will.  If you don't have the means to do such a convertion to OM code, feel free to email me at (c @ sharepointmvp . net) with the Powershell script and I can convert it for you and provide you with the test code.


    I trust that answers your question...

    Thanks
    C

    http://www.cjvandyk.com/blog | LinkedIn | Facebook | Twitter | Quix Utilities for SharePoint | Codeplex

    2012年6月22日 14:22
  • You could wrap your code in "using" statement which should allow the garbage collector to clean them up once you're done with them.  For this kind of job I prefer to write Object Model code in C# rather than scripting it in Powershell.  If that is not an option, you can use C# style code in Powershell to encapsulate much of your work and get some cleanup that way.

    It would be an interesting test to see if converting your code to C# would run better and more efficient than in Powershell.  I bet it will.  If you don't have the means to do such a convertion to OM code, feel free to email me at (c @ sharepointmvp . net) with the Powershell script and I can convert it for you and provide you with the test code.


    I trust that answers your question...

    Thanks
    C

    http://www.cjvandyk.com/blog | LinkedIn | Facebook | Twitter | Quix Utilities for SharePoint | Codeplex

    Thanks for the reply, C.  I'll look into the using statement.  As far as C#, that's a little over my head so I won't be converting it for the time being.  We have a developer in house who I could ask as it's probably against some company policy for me to send the code to an external resource to take a look.  I appreciate you offering though!  I'll let you know how the using statement turns out.
    2012年6月26日 14:10