2

JAXB works fine for marshalling and unmarshalling. Because it can be slow to create marshallers and unmarshallers, using pools for them (based on contextPath) seems a reasonable approach. However, it appears that Unmarshaller holds onto objects after it is done. If it was passed a very large document, then it may hold onto a lot of memory if that particular Unmarshaller isn't reused for a while. Is there a way to cause JAXB to release the objects it processed?

JAXBContext jaxBContext = JAXBContext.newInstance(contextPath, loader);
Unmarshaller unMarshaller = jaxBContext.createUnmarshaller();
...
responseObject = unmarshaller.unmarshal( new StreamSource( new StringReader( xml ) ) );

There are examples online of pooling approaches like this (e.g. one at apache: JAXBUtils.java). Most don't seem to do anything special when they put an Unmarshaller back in the pool.

Update: This appears to be a bug in JAXB: Post-Unmarshall Object Retention. The similar bug in Marshall was fixed earlier so it's in recent versions of JAXB. So now I'm wondering (a) if there's a workaround for this issue with Unmarshaller (b) which version of Java6 this fix is/will be included in.

4
  • What exactly do you mean by "the objects passed to it"? Do you think that the Marshaller holds references to the marshalled objects after it's done with marshalling? Mar 3, 2011 at 22:04
  • Which implementation of JAXB are you using: Metro (the RI), EclipseLink MOXy, Apache JaxMe?
    – bdoughan
    Mar 3, 2011 at 22:18
  • The problem may be the Unmarshaller in the case where the server has returned a 20Mb XML documen that JAXB has processed. Later noticing that the JVM has a lot of memory and it appears to be the JAXB pools that are holding onto it. I believe this is the standard JAXB now bundled with Java 6. Mar 3, 2011 at 22:30
  • 2
    All JAXB implementations that pass the JSR-222 TCK are standard, the Metro implementation is just the one included in the VM by default. There is no reason for an Unmarshaller to retain a reference to the DOM produced, although technically not a bug I would recommend raising the issue on their mailing list.
    – bdoughan
    Mar 3, 2011 at 22:40

3 Answers 3

2

One workaround that occurs to me is to pool the JAXBContext rather than the Un/Marshaller. Doing some quick timings, it seems that the time taken for creating a Un/Marshaller is negligible compared to the time taken to create a JAXBContext from a contextPath. And by holding only a reference to the JAXBContext in the pool, then the Unmarshaller should be freed up, which hopefully allows the GC to reclaim its memory along with that of the doc that the Unmarshaller was holding onto due to the jaxb bug.

0
0

Another possible workaround would seem to be to immediately reuse the UnMarshaller so that it will release the objects from the previous unmarshal. Perhaps pass it a valid but essentially empty XML doc, a string that just has the outermost tags needed. Unfortunately, initial results from this don't seem promising.

String xmlStr = "<ns0:SvcData xmlns:ns0=\"http://Company.com/Schemas/Svc/Base\"></ns0:SvcData>";
Object o = unMarshaller.unmarshal( new StreamSource( new StringReader( xmlStr ) ) );
0
0

http://java.net/jira/browse/JAXB-843

I faced with the same with marshaller. I reported the issue with a test program. If you try to marshal something stupid, you catch and consume the exception like in the attached file in the issue report, then your marshaller releases the previously used resources.

I think it would worth trying the same with unmarshallers :)

3
  • As mentioned in the question, this was reported for the unmarshaller too (later than for the marshaller) and fixed. But the fix is in a later version of JAXB that isn't included in the Java6. So we were looking for a workaround. I think the answer of pooling the JAXBContext rather than the Un/Marshaller works. Jun 17, 2011 at 18:15
  • Could you tell me which is the issue which is reported and fixed? The one above (java.net/jira/browse/JAXB-525) is pretty look like waiting for clarification. Jun 17, 2011 at 20:43
  • Sorry, I misunderstood what you were suggesting. Interesting that catching exception causes it clean up. Jun 18, 2011 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.