how to release the resources of XmlDocument
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: how to release the resources of XmlDocument

Hybrid View

  1. #1
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    how to release the resources of XmlDocument

    When I open an xml file using the XmlDocument.Load function, the memory usage increases a lot.

    The file I'm opening is size of 10MB. When I open this file in a WinApp, the momory usage increases with about 25mb (the file is only 10MB). I have 3 versions of this file, when I open them all 3 (a few times), the memory is increases about 75MB. In my website it even increases from 100MB to 450-500MB.

    Looks like the GC does not collect them. The XmlDocument objects I'm using, are all local variables, thus I know for sure that there is no more reference to the object after the method is executed.

    When I'm correctly informed, the GC collects after the method is executed and the local variables aren't in use any more. But when I force the GC using GC.Collect(), the memory usage is decreased a bit (back to 200MB in website), but never gets back to the original 100MB.
    I also know that the GC.Collect method shouldn't be used, thus I would like to know if there is any other option that releases the resource of the XmlDocument class?

    Am I missing something about the XmlDocument class I should know?

  2. #2
    Join Date
    Nov 2006
    Posts
    357

    Re: how to release the resources of XmlDocument

    Im currently doing some stuff with XDocument on hosting so it would be nice to find out how you solve this one... it sounds like memory leaks (inside the XDocument) or the data is still in use somewhere in your app, but i dont really know that much about it... Could you post a snippit of your code?

    It could *possibly* be down to the file handle not being freed up or something...?

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: how to release the resources of XmlDocument

    The code of my webpage
    Code:
       public partial class WebForm1 : System.Web.UI.Page {
          protected void Page_Load(object sender, EventArgs e) {
    
          }
    
          protected void Button1_Click(object sender, EventArgs e) {
             XmlDocument doc = new XmlDocument();
             doc.Load(Server.MapPath("data/reports/lastmonth/publisher.xml")); //the 10mb xml-file
          }
    
          protected void Button2_Click(object sender, EventArgs e) {
             GC.Collect();
          }
       }
    after some closer looking, the memory even increases almost 100mb for the first call!!

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: how to release the resources of XmlDocument

    The Garbage Collector runs when it sees fit. It does not run at specific times and does not run after a method is called....

    http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx

    The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you use the newoperator to create an object, the runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.

  5. #5
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: how to release the resources of XmlDocument

    So I just should wait untill the memory usage decreases after the GC has decided the collect?


    To come back on what Grofit said:
    it sounds like memory leaks (inside the XDocument)
    Is this possible?

    And why increases the memory with 100MB for a 10MB file?

  6. #6
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: how to release the resources of XmlDocument

    Quote Originally Posted by dannystommen View Post
    So I just should wait untill the memory usage decreases after the GC has decided the collect?


    To come back on what Grofit said:

    Is this possible?

    And why increases the memory with 100MB for a 10MB file?
    Why would you "wait" for it? Are you running code that depends on what the memory usage is?

  7. #7
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: how to release the resources of XmlDocument

    Quote Originally Posted by eclipsed4utoo View Post
    Why would you "wait" for it? Are you running code that depends on what the memory usage is?
    What I meant with that is that I don't need to perform any action and that the memory usage will decrease by itself?

  8. #8
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: how to release the resources of XmlDocument

    Quote Originally Posted by dannystommen View Post
    What I meant with that is that I don't need to perform any action and that the memory usage will decrease by itself?
    correct. that's what the GC is there for.

  9. #9
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: how to release the resources of XmlDocument

    I noticed that the memory usage has decreased back to about 100MB after the night, so looks like the GC does it job after all.

    But still, how is het possible that opening 3 files of 10MB increases the memory usage from 100MB tot about 500MB?

  10. #10
    Join Date
    Apr 2013
    Posts
    1

    Re: how to release the resources of XmlDocument

    I guess it is not correct to compare the physical size of a file on disk to the memory required to open the file using some other process

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center