Search code examples
androidiosxmldomsax

What size xml file should be considered too large for a DOM parser?


I have been developing a fairly simple app for iOS and Android that parses local xml in order to form lists of items and show information about them. When looking into my parsing options, I settled on the native SAX parser for android, as well as the NSXMLparser class that is built in to objective-C. When looking at my options, I repeatedly saw people saying that DOM parsers are not suitable for large xml files. However, no one ever defined exactly what "large" meant. In a later version of the app, I am thinking about switching to a DOM parser.

My question is: Where do you draw the line, and eliminate DOM parsers as an option? The platforms in question are iOS and Android, both of which, of course still have many old devices floating around. So, assuming a slower-than-average device, where is the line drawn?

Thank you for your consideration.


Solution

  • DOM parsers have to load whole XML into memory. Also, it is typical that parsed XML will occupy 5x-10x memory compared to size of original XML.

    If you know that allowed memory consumption of your application is say N megabytes of RAM, then you can draw that line yourself: divide N by 10.

    For example, if you don't want to go over 10MB of RAM, your XML should not exceed 1MB.

    Only most recent devices have 1GB of RAM (divided among all apps), older devices had 512MB and even 256MB. 256MB/10 = 25MB. You probably don't want to eat more than 10% of all memory, so anything over 2.5MB for DOM XML supported on all platforms is way too much.

    But, this is just rule of thumb, and only real life test can tell you the truth.