4

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("http://consotvo.internal.com/upi"))
        {
            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)
                    {
                        Console.WriteLine(wb.Title);


                        //wb.Dispose();
                        //manager.Web.Dispose();
                    }

                }
            }
        }
    }

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.

0

1 Answer 1

6

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.

Edit

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))
        {
            try
            {
                // ...
            }
            finally
            {
                webPartManager.Web.Dispose();
            }
        }
    } // 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.

5
  • it reduced it to 96,720 k now, thanks but don't think it's enough, +1 Jul 8, 2013 at 14:54
  • when code gets into SPLimitedWebPartManager it adds 40,000 to 44,000 making it +90,000. Jul 8, 2013 at 15:18
  • already tried disposing, nothing happens Jul 8, 2013 at 15:18
  • I even tried disposing object in foreach loop but didn't seen any difference, however I will accept answer. thank you Jul 8, 2013 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. Jul 8, 2013 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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