SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this small chunk of code and I can't understand why it's taking soo memory.

    static void Main(string[] args)
        using (SPSite mySite = new SPSite(""))
            using (SPWeb web = mySite.OpenWeb())
                //foreach (SPFile file in web.Files[0])
                //    Console.WriteLine(file.Name);

                SPFile file = web.Files[0];

                using (SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
                    foreach (System.Web.UI.WebControls.WebParts.WebPart wb in manager.WebParts)



This code is consuming 97,868 k even after code has been executed even thought i didn't closed console window yet. my problem is that i want to open 3000 webs (different url) and make changes to a webpart but too much memory is being consumed.

share|improve this question
up vote 6 down vote accepted

I'd guess the culprit would be:

SPFile file = web.Files[0];

This will load the entire collection into memory, even though you are only after a specific file.

Use SPWeb.GetFile() instead.


Additionally, SharePoint 2007 and WSS 3.0 Dispose Patterns by Example provides an example of using the SPLimitedWebPartManager, that requires an explicit Dispose() call on SPLimitedWebPartManager.Web:

using (SPSite siteCollection = new SPSite("http://moss"))
    using (SPWeb web = siteCollection.OpenWeb())
        SPFile page = web.GetFile("Source_Folder_Name/Source_Page");
        using (SPLimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
                // ...
    } // SPWeb object web.Dispose() automatically called
}  // SPSite object siteCollection.Dispose() automatically called

Also, there is a fairly lengthy description of this issue by Bryan Napier here: SPLimitedWebPartManager Memory Leak?

Edit 2

The above SPLimitedWebPartManager.Web memory leak is apparently fixed in SP2010.

share|improve this answer
it reduced it to 96,720 k now, thanks but don't think it's enough, +1 – Mathematics Jul 8 '13 at 14:54
when code gets into SPLimitedWebPartManager it adds 40,000 to 44,000 making it +90,000. – Mathematics Jul 8 '13 at 15:18
already tried disposing, nothing happens – Mathematics Jul 8 '13 at 15:18
I even tried disposing object in foreach loop but didn't seen any difference, however I will accept answer. thank you – Mathematics Jul 8 '13 at 16:03
One thing I would try, with the Manager.Web disposed, after you run the application, check the ULS to see if you get an entry stating that the system cleaned up an item that wasn't disposed. That way you can tell if there's a leak or not. – RJ Cuthbertson Jul 8 '13 at 16:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.