SharePoint Memory Management - Philadelphia Area SharePoint ...

mangledcobwebΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 4 χρόνια και 18 μέρες)

100 εμφανίσεις

Philadelphia Area SharePoint
User Group

www.PhillySharePoint.org

Memory Management in
SharePoint 2007
Development

Matt Vignau

RJB Technical Consulting

www.rjbtech.com

matt.vignau@rjbtech.com

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Overview


In C# .NET development we have the
Garbage Collector


No more destructors


SPSite

and
SPWeb

not always disposed


Leaves growing used memory block


Leads to problems

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Memory Leaks


Build with each use of the code


SPSite

and
SPWeb

sites have ~2kB size
wrappers


Object sizes are closer to 1
-
2 MB


One site with 15 webs accessed twice has
used around 30 MB of system memory; for
*one* user


Can bring down entire farms

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Example 1


public void
GetNavigationInfo
()


{




SPWeb

OurWeb

= http://intranet.litwareinc.com;




foreach
(
SPWeb

OurWeb

in
OurWeb.GetSubWebsForCurrentUser
())




{




//Our
Subsite

code here




}


}


www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Memory Danger
Signs


Does the application pool refresh frequently
when under load?


Memory reset threshold 800 megs
-
1.5 gigs


Does the system perform poorly under heavy
loads?


Does the entire system crash or users receive
“Page not available”?


Does the system use custom
webparts

or third
party
webparts
?

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Example 2


public void
GetNavigationInfo
()


{





SPWeb

OurWeb

= http://intranet.litwareinc.com;





foreach
(
SPWeb

OurWeb

in
OurWeb.GetSubWebsForCurrentUser
())





{





//Our
Subsite

code here





OurWeb.Dispose
();





}


}

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Example 3 pt 1


try


{




SPSite

OurSiteObject
;




SPWeb

OurObject
;




//our main code body here


}


www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Example 3 pt 2


catch


{




//Our exception handling code


}

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Example 3 pt 3


finally


{




if(
OurObject

!= NULL)




{




OurObject.Dispose
();




}




if(
OurSiteObject

!= NULL)




{




OurSiteObject.Dispose
();




}


}

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Breakdown


Try
-
catch block allows for exception
handling


Finally block executes the dispose after
the code block is complete to avoid
problems


No explicit calls to dispose necessary for
SPContext

initializations

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Using statements


One of the most efficient means


Single block usage with automatic
disposal


No additional call out to the dispose
method is needed


Cannot use method outbound transfer of
object beyond the using block

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Example 4


using (
SPWeb

OurWeb

= null){




//Our Code Here;


}

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Caching


Saves on memory


Improves access time


Is not thread
-
safe


IIS is multi
-
threaded


Best for single
-
user applications

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Example 5


public void
CacheData
()


{




SPListItemCollection

oListItems
;




oListItems

=
(
SPListItemCollection
)Cache["
ListItemCacheName
"];




if(
oListItems

== null)




{




oListItems

=
DoQueryToReturnItems
();




Cache.Add
("
ListItemCacheName
",
oListItems
, ..);




}


}

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Example 6


public void
CacheData
()


{




DataTable

oDataTable
;




SPListItemCollection

oListItems
;




lock(this)




{




oDataTable

= (
DataTable
)Cache["
ListItemCacheName
"];




if(
oDataTable

== null)




{




oListItems

=
DoQueryToReturnItems
();




oDataTable

=
oListItems.GetDataTable
();




Cache.Add
("
ListItemCacheName
",
oDataTable
, ..);




}




}

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Scalability


Target scale can impact performance
considerations


Even with proper dispose, memory can
grow


Projected concurrent users are a factor in
your design


www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Scale Questions Pt 1


Is the data static, somewhat static
(occasionally changes), or dynamic (often
changes)?


Is the data the same for all users or does it
change? (Dependent on user account,
department within the company, etc)


Is the data easily accessible or does it
require a long time to return the data?

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Scale Questions Pt 2


Is the data public or does it require a
higher level of security?


What is the size of the data?


Is the SharePoint site on a single server or
on a server farm?

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Links


My Blog: http://blogs.rjbtech.com/MV/


MSDN Best practices: http://msdn2.microsoft.com/en
-
us/library/aa973248.aspx#


Memory Pressure in MOSS:
http://blogs.technet.com/stefan_gossner/archive/2007/11
/26/dealing
-
with
-
memory
-
pressure
-
problems
-
in
-
moss
-
wss.aspx


Memory leak checking tool: http://blogs.rjbtech.com/RJB

www.PhillySharePoint.org

Philadelphia Area SharePoint
User Group

Questions?


Ask away!