Steven De Groote
Steven De Groote

Reputation: 2233

Reading XML and keep DOM model in memory

Due to the very limited documentation of JDOM's detachRootElement, I have doubts about the best way to read an XML and keep the dom tree in memory if that is for read-only purposed.

What's commonly used is something like this:

public class .... {
   Element stored = null;

   private void load() {
    // Build the document with SAX and Xerces, no validation
    SAXBuilder builder = new SAXBuilder();
    // Create the document
    Document doc = builder.build(file);
    stored = doc.getRootElement();
   }
}

(This can throw an exception, but I've left that out for clarity).

Knowing that stored is a class variable, it's unclear if doc will be kept in memory or can be garbage collected after this. If so, is detachRootElement a possible solution to this?

Upvotes: 0

Views: 192

Answers (1)

wolfrevo
wolfrevo

Reputation: 7303

The java garbage collector will not affect any used references. As long as you have a direct or indirect reference to the content of doc, it will not be affected by garbage collection.

Upvotes: 1

Related Questions