tags:

views:

496

answers:

5

I am using JAXB for xml parsing, are there any performance or memory utilization issues?

A: 

I have seen that performance can be hit pretty badly with JAXB as opposed to more 'simple' XML handling mechanisms in Java such as the Xerces SAXParser.

JasonWyatt
+1  A: 

JAXB suffers from the same basic issues as DOM-based parsing, which is that generally speaking, the entire data data structure is held in memory at the same time. That said, it's generally less memory-hungry than a DOM API (with the possible exception of XOM).

Having said that, there are ways of using JAXB to read fragments of large documents in a stream-oriented fashion, if needs be. That's fairly exotic usage, though.

skaffman
can you explain using JAXB as SAX like parser?
Venkat
you don't have to use SAX (an outdated processing model), you can also use STaX or VTD-XML
vtd-xml-author
@Jimmy: https://jaxb.dev.java.net/guide/Dealing_with_large_documents.html#Processing%5Fa%5Fdocument%5Fby%5Fchunk
skaffman
Thank you guys.
Venkat
A: 

JAXB has the additional issue of using reflection to create classes that are added to your perm space as it runs. OutOfMemoryError mayhem can ensue.

duffymo
+2  A: 

One thing to be mindful of is that JAXBContext.newInstance() is a very slow operation. This is where a lot of reflection and class generation happens, leading to perm space issues mentioned by duffymo. Thankfully, JAXBContext is thread-safe, so it's ok to cache one away and reuse it. Otherwise, I think it's safe to say that JAXB memory usage will be on-par (or maybe less) than a full DOM, and, of course, greater than SAX.

If you have very large documents, it's possible to process them in chunks with JAXB. The JAXB RI distribution includes an example of streaming with JAXB.

Dave Ray
A: 

You may indeed run into performance and memory issues with Java XML data binding due to excessive object creation/destruction, this article may help explain a new data binding technique that may help avoiding those issues

vtd-xml-author