programming4us
 
Applications Server
 

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

1/14/2015 8:39:42 PM

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
 
 
REVIEW
 
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox

- Sigma 24mm f/1.4 DG HSM Art

- Canon EF11-24mm f/4L USM

- Creative Sound Blaster Roar 2

- Alienware 17 - Dell's Alienware laptops

- Smartwatch : Wellograph

- Xiaomi Redmi 2
 
VIDEO TUTORIAL
 
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
 
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
 
Top 10
 
- Setup Free Media Server To Stream Videos To DLNA Compatible TV, Xbox 360 & PS3 (Play Station 3)
- How To Install Android Market & Google Apps On Kindle Fire
- How To Make Ubuntu Look Like Windows 7
- How To Add A New Account in MS Outlook 2013
- Get Android & Mac OS X Style Gadgets For Windows 7 & Windows 8 With XWidget
- How To Activate Microsoft Office 2013
- How To Install Actual Facebook App On Kindle Fire
- How To Create, View And Edit Microsoft Office Files On Kindle Fire
- Download Attractive Business PowerPoint Templates For Free At SlideHunter
- How To Use And Enable Hibernate & Sleep Mode In Windows 8