.NET Framework - How to dispose a XDocument?

Asked By shapper
14-Mar-10 08:12 PM
Hello,

I am loading a few XDocuments to read, write, etc.

After using the XDocument cannot I dispose it? I think I should or not?
I was looking for it and XDocument does not have a Dispose method.

Thanks,
Miguel
XDocument
(1)
Dispose
(1)
GC
(1)
Miguel
(1)
Arne
(1)
  Arne_Vajhøj replied to shapper
14-Mar-10 08:18 PM
There are no need for it.

XDocument only contains some data structures in managed memory.

And that can be GC'ed - so no need for any Close/Dispose call.

Arne
Create New Account
help
any strong reference to the images, the items are subject to be discarded by the GC. However, it might not happen at the time you expect it. The GC has to be triggered first. More specifically the generation 2 collector has to be invoked since cached objects often propagated to generation 2. Depending on the kind of GC you use, this might not happen unless the memory is required for something else. The recent .NET runtime has AFAIK 3 different GC implementations. Yes you can force the CG to run. But it is strongly recommended NOT to do so. You might easily confuse the GC, resulting in poor performance. The most critical point is if you invoke the generation 0 / 1 GC too frequently. This will force temporary objects to generation 2. using will not help very resources like open files, database connections, COM objects or something like that. using only call Dispose at the end of the block. It does not free any memory. Marcel GC is not uniformly "better" or "worse". it is "different". However, it _does_ address a particularly
WriteLine("Timer elapsed"); }; timer.Start(); } public static void Main() { { var timerTest = new TimerTest(); } Console.ReadLine(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); / / necessary? does not seem to help Console.ReadLine(); } } Can someone please explain why the Timer classes require that the client code retain a reference, otherwise the object will be GC'ed. But my recollection is that System.Windows.Forms.Timer should continue to work even sample code, since he is using one of the Timer classes that is subject to GC. The answer to that is most likely because he is running a "Debug" version of to null. And setting variables to null only has an effect on object lifetime in GC'ed environments like .NET. In unmanaged code, setting a variable that references a COM object calls to Release() balanced is a major pain in the neck and is why a GC system like what .NET uses is such a boon: specifically because in .NET, setting a objects (on the managed heap) surpasses a certain threshold (which is dynamically calculated while running). ? GC.Collect() is called (the method you use in your code). Allocated objects (in the managed
appear suitable for event-based lifetimes. The IDisposable methodology appears to require explicit calls to dispose the object, to implement the IDisposable interface, and recursively must dispose of all members explicitly. Some popular c# state libraries either do not appear to allow not work (note that in C++ the above is not the same as RAII; the "dispose on assignment" works for a different reason, namely the implementation of the overloaded assignment operator smart-pointer class). Properly encapsulated, it is trivial to add the necessary call to IDisposable.Dispose() when updating the current state reference (e.g. by using a single property or other method to handle the update, and calling Dispose() there). Again note that this has nothing to do with C++-style RAII, as the explicit calls to all member actors in a state that would be placed inside the Dispose(bool) if I used IDisposable. The IDisposable pattern does not automate member finalisation - it must are no longer held, like smart pointers managed by reference counts. The fact that the GC walks reference graphs instead of using ref counting is immaterial to the point - they act void Event2() { currentState_.Event2(); } public void Event3() { currentState_.Event3(); } public void Transition(StateBase newState) { currentState_.Dispose(); currentState_ = newState; } }; } - -- -- -- -- -- - StateBase.cs - -- -- -- -- -- - using System; namespace TestStateMachine { public interface StateBase : IDisposable { void Event1(); void
though related, question: when showing a form, using .ShowDialog, I always call the form's .Dispose method on return. Should I set the form to Nothing as well? Everything seems to any guidance. Gale. VB.NET Discussions VB (1) ShowDialog (1) Component (1) Visual (1) Label2.dispose (1) Label3.dispose (1) IDisposable (1) Dispose (1) Setting an local object to nothing is almost always useless - just as it was unmanaged) that needs cleanup. In those cases you will generally want to make sure that Dispose is called on the object when you are through with it. The easiest way to stuff End If End Using The value of using is that it will ensure that Dispose is called on the object, even if an exception is thrown. It is bascially syntactic sugar for a Try / Finally block. There are objects that implement IDisposable that calling Dispose is not strictly necessary - such as DataSet. These objects actually end up with Dispose because they inherit from component. You may see advice from some that says to avoid
XxxDataReader that will not return any rows - you do not get null. Consistent and logical. Arne LINQ to SQL is a dead end. NHibernate and MS EF are both relevant ORM s to consider. You can use LINQ with both of them. NHibernate is rather sophisticated. Arne There are several free and / or open source tools that can generate mappings and classes way is to generate the tables from the classes not the classes from the tables Arne I do not see the relevance for Peters point. ArrayList implements IList List<> implements IList<> But ToList still creates the second one. Arne PS: Oh - and NHibernate supports both IList and IList<> - you just pick which of them it is not so surprisingly to me that it does the same with no data. Arne There are lots of different solutions that works. The question is what woirk best in working on a 50000 hours projects, then those 10 hours of integration does not matter. Arne If you have an existing database that you need to access by your app, then to define the donkey cart after the Ferrari than the Ferrari after the donkey cart. Arne Given that eager and lazy loading has very specific meaning in ORM context and this is not quite that, then I think the term "execute immediately" is a better term. Arne Yes, perhaps, but it was surprising to me. RL Not true. In traditional NHibernate: .ToList