none
Outofmemory exception on writing XDocument to xmlWriter RRS feed

  • Question

  • hi,

    I need to fix an outofmemory exception that happens on end user machines sometimes. 

    Call stack shows the exception happens on line a) XDocument.Root.WriteTo(xmlWriter)

    Hence thinking of changing this to b) using (XmlReader xmlReader = XDocument.CreateReader())   {  xmlWriter.WriteNode(xmlReader, true);  }

    Will b) be more efficient than a). Is this because xmlReader is basic and avoid complete dom object in memory.

    Thanks

    Wednesday, September 23, 2015 3:42 PM

Answers

All replies

  • I guess it really depends on the size of your XML document and your application. You idea is pretty vague without further information (code).

    I would examine the memory usage and maybe consider a manual GC.Collect() before writing that document.

    Wednesday, September 23, 2015 3:51 PM
  • The line that runs into the OOM, is rarely the line that causes the OOM. First you need to understand what OOM even means:

    Troubleshooting System.OutOfMemoryExceptions in ASP.NET : Microsoft Support Team's IIS Blog : The Official Microsoft IIS Site

    How much memory does my .NET application use? Explaining the Task Manager figure versus Private Bytes in performance monitor.

    We need a lot more data. Ideally some code. Also an idea what you are all encoding into that XML document.

    You either are having a memory leak.
    Or you have files that are a lot bigger in memory then you though. Are you working with images?

    • Marked as answer by sbprasadrao Thursday, September 24, 2015 5:35 PM
    Wednesday, September 23, 2015 5:27 PM
  • Have you also considered XDocument.Save instead of XDocument.Root.WriteTo?

    Wednesday, September 23, 2015 6:00 PM
  • My xmls are big with disk file sizes near 2 MB. They contain only text. 

    XmlWriter in base library (cannot modify) is created as below which internally uses string builder. String builder is probably resulting into framentation as xml becomes bigger 

     using (TextWriter textWriter = new StringWriter())
     using (XmlWriter xmlWriter = new XmlTextWriter(textWriter))

    Came across this article for improving xml performance..

    https://msdn.microsoft.com/en-us/library/ff647804.aspx

    Hence I am assuming xmlWriter.WriteNode(xmlReader, true)  is better alternative. 

    Of course, there could always be other contributing factors for pile up resulting into OOM


    • Edited by sbprasadrao Thursday, September 24, 2015 5:34 PM grammer
    Thursday, September 24, 2015 5:33 PM
  • 2 MiB is not big by any measure. Maybe if you had 400 or so opened at the same time/leak memory. String builder should cause less fragmentation then the alterantive (string connaction). In any case it's use is adviseable from performance point alone.

    Images are a common cause of OOM's. If you want to modify them or even display them you have to undo every compression step applied to them. On disk size can be very misleading, as in memory size equals the same file as bitmap.

    How long is your application kept open normally? How many files are processed with it in that time? Could it be you don't properly release all handles, like putting stuff into a list and never taking it out?

    I think in order to help you we need more then just fragments of your code. Ideally some full blocks.

    Thursday, September 24, 2015 6:20 PM