Reputation: 1019
I need to generate a big xml. I am currently using JAXB 2.0 .
The problem is if I keep all the objects in memory and generate the probability of running out of heap space is very high.
I would like to know the best approach on how to do this.
Thanks
Upvotes: 1
Views: 2660
Reputation: 32407
You can use StAX to marshal the outer body of the XML, and then use JAXB to marshal the repeated objects without having to have them all in memory.
See this thread: http://glassfish.10926.n7.nabble.com/Marshalling-Large-data-into-xml-td55636.html
Upvotes: 2
Reputation: 115328
I'd suggest yo the following.
How do you create your XML? I guess you first create you objects graph, i.e. create all objects and dependencies among them. Then you call something like this:
JAXBContext.newInstance("YOUR PACKAGES LIST").marshal(rootObject, out);
JAXB accesses your objects as beans, i.e. using getters. So, you do not really have to create your full objects graph before marshalling. You just have to provide on-demand implementation of getters, so that they will be able to get the objects upon request. I believe it will help you to decrease the memory consumption.
If you do not want to implement this on-demand mechanism inside your classes you can use AspectJ or other aspect oriented library (or directly byte code modification library like cglib or javassist) to inject the on-demand retrieving code into your model objects.
Upvotes: 0