Hot answers tagged

41

You would never use RootWeb in the context of a using statement and OpenWeb() isn't useful unless the URL specified in the SPSite constructor was a sub web of the site collection in lieu of just the site collection URL. So, you should never do this: using (SPWeb web = site.RootWeb) { // do something with web } ...and OpenWeb() is useful in a scenario ...


14

When you close the PowerShell.exe process, the memory is freed. If you need to dispose objects to keep memory pressure down (important in production environments or if you're looping over all sites/webs), make sure to dispose. If not, you don't need to worry about disposing. The reason why we're so crazy about disposing in the first place is ...


11

Use of objects, such as SPWeb or SPSite, in PowerShell requires a proper memory management. Get commands dispose of these objects immediately after the pipeline finishes, but by using SPAssignment, you can assign the list of objects to a variable and dispose of the objects after they are no longer needed. Here is an example of usage of SPAssignment object, ...


8

In first case SPWeb web = site.RootWeb; SPWeb does not need to be disposed as SPWeb is retrieved from RootWeb. However, in second case SPWeb needs to be explicitly disposed. Mere disposal of SPSite is not enough. So the code should look like: using (SPSite site = new SPSite(SPContext.Current.Web.Url)) { using (SPWeb web = site.OpenWeb()) { } } ...


7

I'm guessing that you are using a regular PowerShell session, probably with the SharePoint snapp-in loaded. And not the 'SharePoint Management Shell'. With a regular powershell session each command is executed with-in its own thread. And disposable objects, like SPWeb, are automatically disposed on the end of a thread. Thus what could happen is that the ...


7

There is no need to dispose of objects using the Client Object Model with JavaScript because it handles the memory management for you. For more information on JavaScript memory management check out this article: https://developer.mozilla.org/en-US/docs/JavaScript/Memory_Management


6

That code will dispose the SPWeb object correctly with the using statement. If an exception is thrown in the method, it will still get disposed.


6

Just to clarify on what you said with that statment, An earlier version of this article recommended that the calling application should dispose of the SPWeb.ParentWeb. This is no longer the official guidance. The dispose cleanup is handled automatically by the SharePoint framework. to my understanding: from where you start the spweb object within ...


5

No, you don't need to dispose the properties, neither you need to dispose the SPWeb or SPSite object, returned using SPItemEventProperties.ListItem Reason: The SPItemEventProperties class internally implements the Dispose() method itself. How: Let's have a look at the source code of the SPItemEventproperties class using the Reflector, to make sure we are ...


5

It got nothing to do with database, when you create a new object it reserves some space or memory in RAM. It is per object, for example when you use SPSite site = new SPSite(URL);it creates a object SPSite in memory. There was a scenario when I had to go through hundred of sub-webs of a site collection and I forgot to dispose SPWeb and it crashed ...


5

No your code isn't leak safe. It'll leak all of the SPSites and SPWebs You need to add a dispose of each SPWeb and SPSite like this: SPSiteCollection siteCollection = webApp.Sites; foreach (SPSite site in siteCollection) { SPWebCollection webCollection = site.AllWebs; foreach (SPWeb web in webCollection) { SPFeature feature = web....


5

It looks like you are writing a server-side object model code in your custom application page that uses SPSite or SPWeb objects but you didn't dispose these objects and this is cause wasting system resources. so try to dispose these objects to release its allocated memory as the following: To dispose SPWeb object, it's preferred to use Using statement as ...


4

Based on the warning you are experiencing, the SPDisposeCheck tool should refer to the following code block: using (SPSite RootSite = new SPSite(WebAppUrl)) { SPWeb RootWeb = RootSite.RootWeb; // CUT ... } Statement: RootWeb := RootSite.{Microsoft.SharePoint.SPSite}get_RootWeb() It would seem that the tool is suggesting that you should ...


4

Yes, this code leads to memory leak. I prefer to use this (or this one for 2007 sharepoint) page every time, when I doubt if object should be disposed. According section SPSiteCollection [ ] Index Operator you must dispose SPSite object. So, in order to correct dispose SPSite in your example, you need to use this code: using (var mySite = site....


4

No. If you do, you might actually get an error, because the using statement under "TheCallingFunction" would call dispose on "web" after "DoSomethingRad" already called "Dispose()". As a rule of thumb, you have to look at your public method as a black box that just does what you expect it to do. To follow the Single-Responsibility Principle just do "...


4

Yes, SPSite needs to be disposed But you can handle the disposal of SPSite through the enumerator using the extension method (see below): static class Extensions { public static IEnumerable<SPSite> AsSafelyDisposed(this SPSiteCollection sites) { foreach (SPSite site in sites) { try ...


4

According to the Bible of SharePoint IDisposable objects, Best Practices: Using Disposable Windows SharePoint Services Objects: SPWeb.ParentWeb Property Updated Guidance An earlier version of this article recommended that the calling application should dispose of the SPWeb.ParentWeb. This is no longer the official guidance. The dispose cleanup ...


4

No, you should not use a using as you're not instantiating the object - you're getting a reference to an existing object. Let the event receiver handle the life cycle of its own objects.


4

Typically disposing isn't much of an issue. If you've got a large farm and are doing a lot of looping you may want to consider disposing after you complete your task for that site/web. The reason it's not much of an issue is because the objects just hold up memory when the PowerShell app is running. Once you close out of PowerShell, the memory those sites/...


3

I would strongly recommend you go through this reference article if you haven't done so already.


3

After some research I found the answer on the general description page of the IDisposable-interface. If an object's Dispose method is called more than once, the object must ignore all calls after the first one. So, if you accidently dispose an SharePoint-object twice, no harm done.


3

I think you have to dispose the SPWeb objects when retrieved from the SPSite.AllWebs collection. If you have look at the implementation of the SPWebCollection class by decompiling it, you can see that everytime a SPWeb is returned, it is instantiated by calling OpenWeb on the SPWebCollection.ISPWebCollectionProvider (which is in this case the SPSite). Thus, ...


3

As Hugh said, in the getSelectedList function you have the first error. using (myWeb) { SPListCollection myLists = myWeb.Lists; foreach (SPList myList in myLists) { if (myList.Title.Equals(ListTitle)) rList = myList; } } myWeb.Dispose(); This will dispose the myWeb istance after the using block. The suggested way from ...


3

There is no need to dispose of objects in the managed client object model, as there are no unmanaged resources in use, everything is cleaned up by the garbage collector.


3

The code you written is Ok. using statement will dispose the SPWeb object you have instantiated. Parent Site - This object is not initiantiated by you, you are using SPSite that belongs to SPContext. SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code. This is true also for the SPSite and SPWeb ...


2

I highly recommend that you download and install the SPDispose Check tool. http://archive.msdn.microsoft.com/SPDisposeCheck You have the option of including this as part of the build process inside of Visual Studio. I've found that it is helpful in finding things that were missed, but also it will point out when it does NOT need to be disposed. The ...


2

If you want to get hold of a subsite using GUID or URL (server-relative or site-relative), OpenWeb() should be used.The SPWeb returned from OpenWeb() should be manually disposed. Also, want to share this for something interesing on OpenWeb() . RootWeb gives you the top most SPWeb associated with SPSite. Although, you can also get it using OpenWeb(), RootWeb ...


2

You would need to share with us the piece where you get a reference to the WEB and Library, related to the Best practices SPWEb/SPSite dispose (e.g. try putting the whole thing into a using {}, make sure you do not dispose SPContext.Current.Web, etc.) As best practice, try checking if library has Content types enabled (ContentTypesEnabled property), then if ...


2

In documentation An earlier version of this article indicated that the calling application should dispose of the SPSite.RootWeb property just before disposing of the SPSite object that is using it. This is no longer the official guidance. The dispose cleanup is handled automatically by the SharePoint framework. Additionally, SPSite properties ...


2

SPDisposeCheck may warn you about undisposed SPWeb you get from SPSite.RootWeb. You need not worry about it. No explicit dispose required for RootWeb. The SPSite you create is disposed by implementing using(). The RootWeb will be disposed by SPSite.


Only top voted, non community-wiki answers of a minimum length are eligible