Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

DOM and memory usage.

843834Aug 3 2004 — edited Aug 4 2004
Consider the following snippet of code which simply reads in an existing XML file:

<code>
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

public class testje {
public static void main(String[] args) {
Document doc;
long begin =System.currentTimeMillis();
try{
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("/home/helios/TEST.xml"));
}catch (Exception e){
System.out.println(e);
}
System.out.println("It took" + (System.currentTimeMillis() - begin)/1000);
}
}
</code>

Now given that the file opened /home/helios/TEST.xml has a filesize of 17 megabyte, I start
the jvm with the following extra parameters: -Xms180M -Xmx180M -verbose:gc giving the
jvm 180 meg of memory to play with and ask verbose garbage collector output. This results
in:

[GC 49152K->36549K(178176K), 0.2038620 secs]
[GC 85701K->73174K(178176K), 0.2440500 secs]
[GC 122326K->109761K(178176K), 0.2438490 secs]
[Full GC 158913K->146408K(178176K), 0.7843300 secs]
[Full GC 178155K->170094K(178176K), 0.6152980 secs]
[Full GC 178175K->176123K(178176K), 0.5497600 secs]
[Full GC 178175K->177655K(178176K), 0.7799180 secs]
[Full GC 178175K->178043K(178176K), 0.5397210 secs]
[Full GC 178160K->178125K(178176K), 0.5340100 secs]
[Full GC 178125K->178121K(178176K), 0.7401240 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Meaning that an 17 meg xml file results in >180 megabyte memory usage, being a factor 10.
How is this possible ? And how can this be avoided ? Are JDOM and DOM4J also eating this
much memory ? (I haven't found a benchmark more recent than 2002).

In the application I'm building it is required to use a builder since these kind of files (>20 meg) also need to get created and additions to these files have to be made. Any suggestions ?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 1 2004
Added on Aug 3 2004
4 comments
464 views