IT tutorials
 
Applications Server
 

Microsoft Sharepoint 2013 : Using SharePoint Commands (part 6) - Disposing of SharePoint Variables

1/14/2015 8:39:42 PM
- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

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.

FIGURE 20

image

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.

FIGURE 21

image

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.

 
Others
 
- Microsoft Sharepoint 2013 : Using SharePoint Commands (part 5) - Working with Objects Below the Web Level
- Microsoft Sharepoint 2013 : Using SharePoint Commands (part 4) - Working with Webs
- Microsoft Sharepoint 2013 : Using SharePoint Commands (part 3) - Working with Site Collections
- Microsoft Sharepoint 2013 : Using SharePoint Commands (part 2) - Web Applications
- Microsoft Sharepoint 2013 : Using SharePoint Commands (part 1) - Working with the Farm, Working with the Farm Configuration
- Microsoft LynServer 2013 : Dependent Services and SQL - Office Web Apps Server
- Microsoft LynServer 2013 : Dependent Services and SQL - Network Dependencies (part 2) - Defining Network Sites
- Microsoft LynServer 2013 : Dependent Services and SQL - Network Dependencies (part 1) - Supporting Lync Phone Edition with DHCP
- Microsoft Exchange Server 2013 : Designing a Successful Exchange Storage Solution (part 3) - Selecting the Right Storage Hardware, Storage Validation Using Jetstress
- Microsoft Exchange Server 2013 : Designing a Successful Exchange Storage Solution (part 2) - Making Sense of the Exchange Mailbox Server Role Requirements Calculator
 
Youtube channel
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS