As shown in the code below I end up creating a new PSObject for each site in the sites collection. Eventually I need to run this for multiple SharePoint web applications each containing several sites, with the total sites crossing 2000. So that is a lot of PSObjects being created.
The PSObject does not have a dispose function so what are my options to make the code more efficient and dispose the object.
Also each time i execute the operation $SiteInfoCollection += $object
there is reallocation of memory since it is a dynamic expanding array. According to some article i read several months ago any operation of memory reallocation is expensive and not good for performance. Is there a way to make this step efficient as well?
$SPWebApp = Get-SPWebApplication "http://portal.contoso.com"
$SiteInfoCollection = @()
foreach($site in $SPWebApp.sites)
{
$object = New-Object PSObject
Add-Member -InputObject $object -MemberType NoteProperty -Name URL -Value ""
Add-Member -InputObject $object -MemberType NoteProperty -Name Title -Value ""
Add-Member -InputObject $object -MemberType NoteProperty -Name Template -Value ""
$object.url = $site.url
$object.title = $site.rootweb.title
$object.template = $site.rootweb.WebTemplate
$SiteInfoCollection += $object
#$object.Dispose() #NOT valid operation
}
$site.Dispose()
}
$SiteInfoCollection