JDOM2 Feature Reduced Memory Footprint

Rolf edited this page Apr 1, 2012 · 3 revisions

Small changes have been made to reduce the amount of memory required to load a JDOM document in to memory. Additionally, a special JDOMFactory instance, the SlimJDOMFactory, has been created to enable further memory reductions.

The major changes are:

  1. The structure containing Attribute values for an Element is only created if needed. Many Elements have no attributes, and creating a List structure for every Element (even if it has no Attributes) is a waste. JDOM2 only creates the List of Attributes if it is needed.
  2. Element now has the method hasAttributes() to check whether the Element has attributes. This allows users to check whether there are attributes without having to initialize the Attribute List if it was not yet initialized. All references to the Element's Attributes in JDOM code have been modified to ensure that hasAttributes() is true before needlessly initializing the empty Attribute List. This saves memory.
  3. JDOM2 introduces StringBin, and SlimJDOMFactory. StringBin is a class that allows String values to be 'cached', and then reused. Many XML string values are repeated, e.g. Element names, Attribute Names, Attribute values, String indenting in the XML, and many other values. The SAX parsing process, and other processes seldom ensure that String instances are reused, instead creating new String instances for values that previously existed. The SlimJDOMFactory keeps a reference to each unique String value, and reuses previously used String instances.

The Attribute container change will never increase memory usage, and saves about 10% of memory in 'typical' XML usage.

The SlimJDOMFactory can save much more memory, but comes at a performance cost. The combination of the Attribute and the StringBin change can reduce the memory footprint by about 20%.