3

I've created a new project in java with this simple code:

    public static void main(String[] args)
{
    JFrame frame;
    frame = new JFrame("Empty");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

I've noticed that moving the JFrame causing the memory used by the process to increase.

  1. Why is that?
  2. Is there a way to avoid this using the same code above but with some additions?
  3. Are there any better ways to simply display a JFrame?
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Raz Cohen
  • 540
  • 4
  • 15

1 Answers1

3

Seeing memory usage increase does not mean that there's a memory leak. The program may use more memory because it has to create temporary objects for event dispatching or repainting. These temporary objects are short-lived and are removed by the garbage collector in a short period of time; so the memory used by them becomes available for the program again.

You won't see this with process monitoring tools though since the memory is not returned to the OS; the JVM reserves it for future use. You can use tools like VisualVM to monitor the actual memory use of the program.

Are there any better ways to simply display a JFrame?

The code that you post is actually incorrect; you shouldn't create and manipulate GUI objects from the program's main thread. Here's a correct example of displaying a JFrame from the Java Tutorial:

import javax.swing.*;        

public class HelloWorldSwing {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Joni
  • 108,737
  • 14
  • 143
  • 193
  • Thanks for the answer. I did what you suggested (changing the code and using VisualVM) and I noticed the heap usage is going up, even without moving the JFrame. Is it normal? Here's a [link](http://oi42.tinypic.com/wjx7rm.jpg). – Raz Cohen Sep 23 '13 at 09:11
  • That looks a little suspicious but since the program has lots of free memory the GC probably does not bother doing a resource-intensive full garbage collection. You should see a drop in heap use if you push the "perform gc" button, or run the program with a smaller heap for a longer time. – Joni Sep 23 '13 at 10:08
  • You can install the visualgc or memory pool plugin to see better what's going on: you would see that the youngest generation is fully cleared on each small GC while objects slowly accumulate in the older generations. A less frequent full GC will remove objects from the older generations. – Joni Sep 23 '13 at 11:35