Destroying XMLNode and XMLElement objects

Posted on 2006-05-11
Last Modified: 2006-11-18
I have a program where we create a very large (50MB+) XML file using C#.  It is taking a huge amount of memory in doing this.  It had become clear to use that the unreferenced objects were not being cleaned up and this the memory usage just kept growing.

We built a test program to verify this:

         * public void LoadDocument()
         * --------------------------------------------------------------------*/
        public void LoadDocument()
            XmlDocument m_XmlData;
            XmlNode testnode;
            XmlElement testelement;

            m_XmlData = new XmlDocument();
            m_XmlData.LoadXml("<Test />");

            for (int i = 0; i < 2300; i++)
                Trace.WriteLine("Running Loop " + i.ToString());

                testnode = m_XmlData.CreateElement("Test");
                testelement = m_XmlData.CreateElement("TestElement");
                testelement.InnerText = "Hi There";



This function is called in a thread and when executed allocates about 4MB to preform.  When the function is done, and it exits the thread ends also.  However the memory allocation remains unchanged.  The memory allocated is not released.  This confirms our thought that memory was being allocated but not freed.   I know that garbage collection does not happen right away but how can I get these XMLNode and XMLElement objects to free after each interation of the loop?

When we run our larger XML file creator, it will create the 50 MB XML file but will allocate almost 1GB of RAM to do it.  It is hugely disproportionate.

We have tried having the garbage collection run at the end of each loop using GC.Collect() but it had no effect.

Any ideas out there?
Question by:jymmealey
    LVL 11

    Accepted Solution

    you should not create your xml structure in memory first.
    write it directly into the file by using the XmlWriter.
    LVL 2

    Author Comment

    Writing it directly to the file is not an option for us at this point.  It would mean heavily re-writing and that can't happen right now.  If I can get the memory issue resolved there should be no need.

    LVL 11

    Expert Comment

    you cant solve that memoryissue because you are bulding a tree of xmlnodes internally.
    so you have dependencies between parent nodes and childnodes.
    what you could try to do is splitting your structure into logical blocks and create a list of references to memorystreams.
    then in your loop, create the logical block with a reference to its parent node, write that block into a memorystream and compress that stream.
    after creating your data itereate through your memorystream list, decrompress each memorystreamobject and write it to a file.
    thats only an appproach.
    LVL 2

    Author Comment

    I think vo1d is correct although I have not been able to fully explore all options.  As created elements are added to the document it seems to create some kind of lasting reference, although the object is no longer directly accessed by the document.  I have tested several times by chaning the element object after it is added and it make no effect on the document so it seems silly that the reference holds, but none the less it does.

    I am using the XMLWriter, I had to alter the program a bit but I think it will work fine.


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now