SP2013 Create site collection leads to memory leaks

Why does this simple loop leads to a memory leak when using the SP 2013 server api?

const int INT_Constant = 5;
private static void CreateSC() {
		Microsoft.SharePoint.Administration.SPWebApplication app = Microsoft.SharePoint.Administration.SPWebApplication.Lookup(new Uri("http://demosrv"));
	for (int i = 0; i < INT_Constant; i++) {
		Console.WriteLine(" Create {0}", i);
		using (SPSite newSite = app.Sites.Add("/sites/memory" + i, "demo\\administrator", null)) {
		}
	}
}

When i analyze the memory with a memory profile it seems that the Microsoft.Sharepoint.Administration.SPVolatileCache keeps the spsite entries in memory...

Thanks

Marco

May 19th, 2015 11:38am

It seems that adding:

GC.Collect();
GC.WaitForPendingFinalizers();

helps releasing the memory.. It that the correct solution?

Free Windows Admin Tool Kit Click here and download it now
May 19th, 2015 1:32pm

I would never use GC in any SharePoint production code. As long as you properly dispose of objects, SharePoint and .Net do a very good job of memory management. SharePoint does cache object on its own, and the .Net GC does its "thing" pretty much on its own schedule.

The memory you are seeing may be from what is being cached by the web application, and that is a managed object with no .Dispose().

I repeated your example for 100 site collections and watched RAM bounce between  62MB and 83MB while it ran. So I did not see a memory leak.

319040   (RAM before starting the loop)

 Create 0
68620608
 Create 1
63278392
 Create 2
68393888
 Create 3
62505208
 Create 4
61946624
 Create 5
69653432
 Create 6
74404024
 Create 7
65080184
 Create 8
69989136
 Create 9
68454144
 Create 10
64471600
 Create 11
65255568
 Create 12
62427384
 Create 13
67838496
 Create 14
69790560
 Create 15
72153976
 Create 16
77695144
 Create 17
73630112
 Create 18
68658040
 Create 19
75349928
 Create 20
68309336
 Create 21
69946312
 Create 22
72754944
 Create 23
73090336
 Create 24
71795840
 Create 25

May 19th, 2015 4:44pm

But according to your memory stats it is increasing.. You start with 68620608 and after 25 new sitecollections it's 71795840... 

for a migrating we need to create many many sitecollection and our memory consumption is increasing to 10gb.. We can't find any not properly disposed SPSite or SPWeb (even with a memory detection tool). Analysing the memory leads to many SPSite objects that are hold in the Microsoft.Sharepoint.Administration.SPVolatileCache..

Free Windows Admin Tool Kit Click here and download it now
May 20th, 2015 2:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics