JAXBContext initialization slows down application performance.
Slow performance in WebSphere Application Server.
JAXB context (javax.xml.bind.JAXBContext) object instantiation is a resource intensive operation. JAXB Context instantiation involves the pre-load and pre-creation of contexts (called the pre-caching process) of all packages and classes associated with the context, and then all of the packages and classes which are statically (directly and indirectly) referenced from those. Performance latency will correlate with the number of classes which are passed during JAXB creation during this pre-caching process.
Diagnosing The Problem
Thread dumps collected during performance latency will show many instances of "JAXBContext.newInstance".
Resolving The Problem
Singleton JAXB Implementation:
If an application requires heavy use of a single JAXBContext, the application should implement the singleton model of the JAXBContext instance. In the singleton model, re-using a particular JAXB context (which is bound to a set of classes/packages) in a repeated manner will drastically reduce JVM effort on pre-load and pre-creation of contexts..
For example, implement a get() method which only returns a single instance of the JAXB context. Then re-use this reference wherever this context is required.
newInstance() JAXB Implementation
If an application requires the use of heterogenous JAXBContexts (contexts which are bound to different classes / packages) where each context operates in a limited manner, the application will need to make use of multiple JAXBContext instances. Implementing JAXBContext.newInstance() creates a new JAXBContext at each invocation. Heavy use of JAXBContext.newInstance() comes with the performance trade-off caused by the repeated context initialization. The effect of the initialization delay is directly correlated to the number of JAXBContext objects created.
Disabling Pre-Caching And Optimization Mechanisms
With each JAXBContext initialization, pre-caching and optimization mechanisms can be disabled by specifying specific JVM System Properties (see below). Please note that when the pre-caching mechanism is disabled, the runtime marshaller / un-marshaller using the pre-cached metadata will be impacted adversely. Here the rule-of-thumb for determining an appropriate pre-caching feature for an application is based on the re-usability of the JAXB context(s):
- If the re-use factor is high, enable pre-caching (a slow boot, but boosted runtime performance).
- For heterogenous and non-repeatable JAXB operations, disable pre-caching to gain boot time performance.
JVM System Properties to Disable Pre-Caching
- WAS 7.0 users (JAXB within JDK6 bundled by WAS):
Liberty Users :
*see below for related JAXB documentation
15 June 2018