[jdom-interest] memory question?

Jason Hunter jhunter at acm.org
Sun Jan 7 22:30:30 PST 2001


I'm seeing similar behavior.  But watching in OptimizeIt I see the
objects do get collected when memory maxes out.  Seems GC just isn't
eager to collect the objects until it's necessary.  If I run the test on
a large file (enough to get memory mostly consumed) and then a medium
sized file (just enough to poke memory usage over the max) I see very
little memory used afterward.

I'm going to chalk it up to weird GC behavior unless someone has ideas.

BTW, I'm using Sun's JDK 1.2.2 on Win2K.  You too?

-jh-

> Guanming Wu wrote:
> 
> 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
>





More information about the jdom-interest mailing list