Does PowerShell STA mode eliminate SharePoint memory leak issue?

By : dahlbyk
Source: Stackoverflow.com
Question!

Some background:

In short, standard SharePoint guidance is that COM-backed objects like SPSite and SPWeb should not be used by different threads. This stands in conflict with PowerShell's use of MTA mode by default, verified in the Leak Workarounds post referenced above. One suggested workaround was to try PowerShell 2.0's -STA flag, which seems like it should solve the problem; however, in the comments on his post Zach suggests STA mode is not enough.

This pushes to the edge of my COM knowledge, so I'm hoping someone can help me understand...

  1. Should STA mode be sufficient to keep object access limited to a single thread across PowerShell pipelines?
  2. If not, why?
By : dahlbyk


Answers

Ultimately, -STA mode should be enough provided you are using Powershell 2.0. The reason for this is that in STA mode, the default runspace reuses a single thread for all interactive commands (and scripts too). It's possible that the version of powershell that Zach was looking at in february behaved differently that the current RC/RTM of PowerShell 2.0. It may have used UseNewThread instead of the current default, ReUseThread:

PS
By : x0n


This video can help you solving your question :)
By: admin