[jdom-interest] memory question?

Guanming Wu wugm at cshl.org
Wed Jan 3 17:34:55 PST 2001


It seems to me that it is very difficult for garbage collector (gc) to collect jdom document and element objects. Here is a test program:

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;

public class XMLMemoryTest extends Object {

    public XMLMemoryTest(String file) {
        loadXML(file);
    }

    private void loadXML(String file) {
        try {
            SAXBuilder builder = new SAXBuilder(false);
            Document doc = builder.build(new File(file));
            Element root = doc.getRootElement();
            root = null;
            doc = null;
            file = null;
            gc();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
     /** This method is from the book Java Platform Performace by Wilson, S & Kesselman, J. */
     private void gc() {
        try {
            System.gc();
            Thread.currentThread().sleep(100);
            System.runFinalization();
            Thread.currentThread().sleep(100);
            System.gc();
            Thread.currentThread().sleep(100);
            System.runFinalization();
            Thread.currentThread().sleep(100);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

import java.io.*;

public class XMLMemoryTestRunner extends Object {

    public static void main(String args[]) {
        if (args.length < 1) {
            System.out.println("Usage java test.XMLMemoryTest xmlfile");
            return;
        }
        memoryInfo("Before loading: ");
        XMLMemoryTest test = new XMLMemoryTest(args[0]);
        test = null;
        memoryInfo("After loading: ");
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String line = null;
            while (true) {
                line = reader.readLine();
                if (line.equalsIgnoreCase("quit")) break;
                if (line.equalsIgnoreCase("check")) memoryInfo("Memory: ");
            }
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
    
    protected static void memoryInfo(String label) {
        long total = Runtime.getRuntime().totalMemory();
        long free = Runtime.getRuntime().freeMemory();
        System.out.println(label + " total " + total + " usage " + (total - free));
    }
}

Compile them and run XMLMemoryTestRunner.

I tested with an 1M XML file, the memory about 8000M could not be released. I am wondering if there are any problems with my codes?

Thanks.

Guanming

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20010103/2ffe86aa/attachment.htm


More information about the jdom-interest mailing list