Java Memory Questions (AWT, applets)
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Pete_W
Posted On:   Tuesday, October 15, 2002 11:27 AM

I have a program where it creates new AWT Frames. The problem is that when each frame is destroyed, the memory is not returned. Eventually, when enough frames have been opened and closed, the machine becomes very slow and nearly unusable. However, I am not getting an out of memory exception. I'd like to know how I should be freeing this memory? I'm using the dispose method, then setting the frame to null, then suggesting the garbage collector to run by doing System.gc() and Runtime.gc(), but the memory never decreases. (I am measuring the memory by viewing the task manager) Any suggestions on what I should try? I tried downloading some memory profiling programs like JProbe, but they don't seem to work with Applets. (   More>>

I have a program where it creates new AWT Frames. The problem is that when each frame is destroyed, the memory is not returned. Eventually, when enough frames have been opened and closed, the machine becomes very slow and nearly unusable. However, I am not getting an out of memory exception.


I'd like to know how I should be freeing this memory? I'm using the dispose method, then setting the frame to null, then suggesting the garbage collector to run by doing System.gc() and Runtime.gc(), but the memory never decreases. (I am measuring the memory by viewing the task manager)


Any suggestions on what I should try? I tried downloading some memory profiling programs like JProbe, but they don't seem to work with Applets. (The Frames are launched by applets)


Thanks,

Pete

   <<Less

Re: Java Memory Questions (AWT, applets)

Posted By:   Frederic_Fondement  
Posted On:   Friday, October 18, 2002 03:01 AM

Each pointer to your frame and included components must be freed. Have you taken care of listeners ? If a listener is referenced in you application, the garbage collector will not return the memory.


A simple gc is often not enough to free memory. Try:


private void gc () {

Runtime runtime = Runtime.getRuntime();

long totalMemory;

long freeMemory;

do {

totalMemory = runtime.totalMemory();

freeMemory = runtime.freeMemory();

runtime.gc();

runtime.runFinalization();

} while(runtime.totalMemory() < totalMemory || runtime.freeMemory() >= freeMemory + (long)4096);

}



If the memory does not decreases, that is because the JVM does not give it back to the system (depends on your JVM and your system), but, once freed, it should be reused for the application... Some tests on my machine gave this:


public static void main (String [] args) {

Runtime runtime = Runtime.getRuntime();

Frame f;

Label l;

long mem, max = 0, first = 0;

for (int i = 0; i < 1000; ++i) {

f = new Frame();

mem = runtime.totalMemory() - runtime.freeMemory();

if (max < mem)
max = mem;

l = new Label(Long.toString(mem) + " " + Integer.toString(i));

f.add(l);

f.show();

f.pack();

if (first == 0)

first = mem;
f.dispose();

f = null;

}

gc();

mem = runtime.totalMemory() - runtime.freeMemory();

System.out.println("Max is " + Long.toString(max) + " first is " + Long.toString(first) + " and now " + mem);

}

Max is 1990080 first is 750480 and now 423464

Re: Java Memory Questions (AWT, applets)

Posted By:   bruce_kdkdfhhghdjskajsdfh  
Posted On:   Tuesday, October 15, 2002 01:04 PM

How are you calling your AWT Windows? can you post the necessary code? It could be that the way you are calling your windows isn't clean or has to be cleaned up after the window is opened or closed to free memory.
About | Sitemap | Contact