Groups | Blog | Home
all groups > dotnet xml > june 2005 >

dotnet xml : Long duration of call XmlDataDocument.CloneNode


Petr Felzmann
6/21/2005 2:11:01 AM
Hi,

why the duration of call the CloneNode(true) is steadily increasing? First
call is 0.004s and 100th is 0.16s! XML file is 5kB only, no disk swap.

XmlDataDocument xml = new XmlDataDocument();
xml.Load(@"C:\foo.xml");
for(int i = 0; i < 100; i++)
{
XmlNode node = xml.CloneNode(true);
}

Whole code for youre test (copy+paste):

using System.Xml;
using System.Runtime.InteropServices;

class Class1
{
[DllImport("kernel32.dll")]
internal extern static short QueryPerformanceCounter(ref
long lpPerformanceCount);
[DllImport("kernel32.dll")]
internal extern static short QueryPerformanceFrequency(ref
long lpFrequency);

public static void CloneNodeStressTest()
{
long start = 0;
long stop = 0;
long frequency = long.MaxValue;

QueryPerformanceFrequency(ref frequency);

XmlDataDocument xml = new XmlDataDocument();
xml.Load(@"C:\foo.xml");

for(int i = 0; i < 100; i++)
{
QueryPerformanceCounter(ref start);

XmlNode node = xml.CloneNode(true);

QueryPerformanceCounter(ref stop);

double elapsed = (stop - start) / (double)frequency;
Console.WriteLine(i + "=" + elapsed);
start = 0;
stop = 0;
}
}

[STAThread]
static void Main(string[] args)
{
CloneNodeStressTest();
}
}

Andy
6/21/2005 7:41:10 AM
My guess is that each iteration of the loop eats a bit more memory.
Remember that the framework does garbage collection, so not all 'lost'
references are freed right away. Also, it may be doing garbage
collection while your loop is executing, which would take some time
from your program as well.

Finally, you're calling out to unmanaged code, and breaking the barrier
between managed and unmanaged code has a significant peformance hit and
maybe the GC has to do some clean up there too, or perhaps there is a
memory leak in the unmanged code.

One thing you could do is instead of using the PerfCounters, just use
two DateTime's, and subtract them. If you still find the same
behavior, its probably due to the garbage collection.

Andy
AddThis Social Bookmark Button