Re: Remove JFrame from memory
On 6/28/2012 9:48 AM, Jesper Johnsen wrote:
How do I remove an object lets say a JFrame from memory?
I know that the garbage collector handles this - but this simple example does not release itself...
java.exe uses 10mb in the first wait stage, this increases to 20mb when the JFrame is shown, but the memory usage never returns to the initial 10mb.
So the garbage collector never removes the object from memory - why?
package jframetest;
import javax.swing.JFrame;
public class JFrameTest {
public static void main(String[] args) {
try{
Thread.sleep(5000);
}catch(Exception Ex){}
JFrame frame = new JFrame("Test");
frame.setVisible(true);
try{
Thread.sleep(5000);
}catch(Exception Ex){}
frame.setVisible(false);
frame.dispose();
frame = null;
while(1==1){
try{
Thread.sleep(100);
}catch(Exception Ex){}
}
}
}
Your program's final loop is pretty much a no-op, and probably
doesn't create many new objects -- it may create none at all. If
the program doesn't use up memory for new objects, the memory it
already has is sufficient and there is no reason for the garbage
collector to run around trying to find more.
Even if collection occurs and the JFrame's memory is recycled,
Java is likely to retain that memory to re-use for other new objects.
If you look at Java's memory consumption "from the outside," with
an O/S tool of some kind, you cannot tell the difference between
memory occupied by Java's objects and memory that currently holds no
objects (but is available for making new ones). The JFrame may or
may not have been collected; you cannot tell from outside Java.
You could use command-line flags to get Java to tell you about
what the garbage collector does. The flags tend to vary from one
version to the next (because the collector changes, too), and you
probably can't discover which particular objects are and aren't
collected. But you can at least see whether GC runs at all after
you dispose the JFrame.
Another possibility would be to change your program a little
so it informs you when the JFrame is collected. One way to do this
would be to make your own subclass of JFrame, with a finalize()
method that alerts you when it's collected:
class MyFrame extends JFrame {
MyFrame(String title) {
super(title);
}
// I do not usually recommend implementing finalize(),
// but here we're just using it for exploration.
protected void finalize() throws Throwable {
try {
System.err.println("Finalizing " + this);
} finally {
super.finalize();
}
}
}
Then you can use `JFrame frame = new MyFrame("test");' and run
the program; if the garbage collector reaps the frame, you will
see a message on the standard error stream. As I mentioned above,
the garbage collector might not run at all if the program consumes
little memory, so you might want to burn some by creating objects
in your final loop, something like:
while (true) {
byte[] junk = new byte[1000000];
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
// ignore
}
}
With this in place you should see Java's memory consumption rise
at the rate of about 10Mbyte/sec, which should provoke garbage
collection fairly soon. *Then* if you don't see the MyFrame get
finalized there may be a problem to look into -- but so far, you
don't have evidence that anything's wrong.
--
Eric Sosman
esosman@ieee-dot-org.invalid