1

This is probably along the lines of .NET Collections and the Large Object Heap (LOH)

In .Net, I'm loading an XmlDocument with a string that makes ~200KB text document when the xml is converted to base64. The point being, the string should be allocated to the large object heap. I know from reading comparisons here that the XmlReader is the most efficient way to read the string, but the XmlDocument probably gives me the more straight forward read, with more functionality (xpath).

Each node of my XML should be a fairly small string, nothing near heading to the large object heap. Using Lutz .Net Reflector it appears the XmlDocument uses linked nodes internally.

So finally, my question: Will loading this string that saves ~200 KB (>85000 Bytes) cause yet another object to the LOH when using XmlDocument. We're a little worried about fragmenting the heap and leading to OOM errors. Or does the XmlDocument just happen to (at least for the case of data I'm asking about) create a lot of objects to the managed heap?

Community
  • 1
  • 1
John Hoven
  • 4,085
  • 2
  • 28
  • 32

2 Answers2

3

It's only object of continous data that is larger than 85 kB that ends up in the large objects heap. For example large strings and arrays with ten thousands of elements.

An XmlDocument consists of a lot of small objects, so it will very rarely allocate anything on the large objects heap. The only chance for that is if a node contains tens of thousande of children, or if a value is longer than 42500 characters.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

Why are you loading the XML into a string to start with? Where is the data coming from? Can't you pass that (e.g. a Stream or TextReader) directly into XmlDocument to start with?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The string is coming from the database. We're building a vertical market app on top of the Microsoft Dynamics platform so using other type of database fields isn't an option for me. – John Hoven Jul 17 '09 at 20:29
  • To be more clear, the string is coming from the Dynamics web service, which pulled it from the database. – John Hoven Jul 17 '09 at 20:30
  • If it's coming from a web service, are you fetching it in an HTTP request? If you have control over that, you may be able to stream the result - if not, you're basically committed to having that 200K string in memory anyway. – Jon Skeet Jul 17 '09 at 20:47
  • I am fetching in an HTTP Request. Streaming the result is not something I'm familiar with, but I'll have to consider. I understand I may be committed to having that string in memory. I'm more concerned that I'm not adding on to that by using the XmlDocument class. Thanks for the response by the way. – John Hoven Jul 17 '09 at 21:01
  • XmlDocument will certainly add to it, but I very much doubt that you'll end up with anything else in the LOH. – Jon Skeet Jul 17 '09 at 21:06
  • OK, what I meant by "add to it" was add to the LOH. Thank you. – John Hoven Jul 17 '09 at 21:12