Disposing of SharePoint Variables
As covered earlier, certain objects
need to be disposed of properly in order to protect your server from
dreaded memory leaks. This section discusses a couple of different
methods for proper disposal of your objects.
The disposal issue becomes a problem as soon as you start to capture certain SharePoint objects, such as SPSite, SPSiteAdministration, and SPWeb, and hold on to them. They cannot be disposed of at the end of the pipeline because you are still using the object.
Luckily, there are two commands to help you work with situations in which you might run into disposal issues: Start-SPAssignment and Stop-SPAssignment. These commands help you to both track objects and then dispose of them when they are no longer needed.
This section describes two ways to use these commands. The first method is the simple assignment method, which uses the -Global switch parameter. Basically, before you start to use objects that might need to be disposed of, you call Start-SPAssignment using the -Global
switch parameter. This starts the tracking of all resources being used,
which, quite frankly, can be a lot. Once you are done working with your
variables and objects, you need to call Stop-SPAssignment with the same -Global switch parameter. At this point, all the objects that were tracked will be released and properly disposed of. Once you call Stop-SPAssignment
you should not use the resources for that block of commands, as the
contents of the variables will have been disposed of and may lead to
unexpected behavior in your script.
Figure 20 demonstrates the use of Start-SPAssignment and Stop-SPAssignment with the -Global switch parameter.
While using the simple assignment method is easy, it does have some drawbacks. Any trackable SharePoint object between the Start-SPAssignment and Stop-SPAssignment
will be managed by the commands. Therefore, if you run many one-liners
that do not necessarily need to be tracked, they will be tracked
anyway, which means more memory that is waiting to be released.
If you know when you need to track a SharePoint
object, you can manually assign your resources to be tracked. This
enables you to be selective regarding the objects that are assigned for
tracking and disposal. You can do this with the same Start-SPAssignment and Stop-SPAssignment commands. With this technique, you create a new SPAssignmentCollection using Start-SPAssignment — for example, $spAssign = Start-SPAssignment.
When you need to track objects, you can use $spAssign in the pipeline. For example, you can assign all webs from a particular site collection for tracking and disposal:
$Webs = $spAssign | Get-SPSite http://portal.contoso.com | get-SPWeb
You can also throw in a few one-liners as you
have been doing up until now. These do not need to be tracked, as they
will be disposed of properly. Because these objects will not be
assigned to the SPAssignmentCollection
object, they will be disposed of at the end of their lifetime and will
not hold on to extra memory. Once you are done with your block of
commands, you can clean up using the Stop-SPAssignment command, passing in the $spAssign variable. Like the simple assignment, once you call Stop-SPAssignment you should not use the variables that were assigned to the SPAssignmentCollection. Figure 21 demonstrates the assignment of a collection of web objects and their proper disposal with the call to Stop-SPAssignment.
Note in Figure 21 that you do not assign the SPWeb object you used to display the SPList objects. This SPWeb object is in a one-liner and will be disposed of correctly without the need for any tracking.