Simple Java Program Increasingly Consuming Memory
Asked Answered
E

3

3

I have this simple Java Code which creates a single JFrame instance and displays it. This link contains the screenshot of memory consumption graph taken by jconsole

enter image description here

What worries me is that java.exe in task manager shows memory usage continuously increasing at the rate of 4-5 kbs every 8-9 seconds. Need help

import javax.swing.*;

class MyGUI extends JFrame
{
    public void makeGUI()
    {
        setLayout(null);
        setSize(500, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

public class Launcher
{
    public static void main(String []args)
    {
        SwingUtilities.invokeLater(new Runnable()
                    {
                       public void run()
                       {
                        new MyGUI().makeGUI();
                       }
                    });
    }
}
Erelia answered 3/10, 2012 at 14:52 Comment(3)
Just so you know, you should rarely have to extend JFrame. And why is this worrying you? Its such a small amount of memory and it probably relates to the internal workings of Swing, or the JVM. It's not like its leaking memory.Flowerpot
JConsole's polling for data does actually lead to heap memory usage. So there is some noise from measuring. If running a GC frees the memory again, it all looks cool.Brassware
I think those are more like instances created by the JVM itself that the Garbage Collector erases from time to time, and it's not really due to your application's instances.Kitkitchen
C
7

The profile looks perfectly normal - the program creates objects and from time to time, the garbage collector frees memory by getting rid of the objects that are not reachable any longer.

The important observation is that the trough points are all more or less at the same level so it does not look like your code has a memory management issue.

You could reduce the height of the peaks by setting the maximum amount of heap space to a lower level but 5 MB is not much anyway...

Corrigible answered 3/10, 2012 at 14:56 Comment(1)
+1 See also this contrasting example.Cud
B
4

I think that this memory is due to the generation of the objects used by swing, like the various UI events (mouse movement, etc...). Swing tends to generate objects for every events and call the listeners handling those events. After that these event-related objects are not used anymore (except if you keep reference to them).

This is not a memory leak, it's normal behaviour. In fact, in your screenshot of the memory consumption, the memory fall sharply when the garbage collector free these objects.

Betty answered 3/10, 2012 at 14:58 Comment(0)
B
0

Java tends to use memory as it needs to. It doesn't garbage collect very often and so it will naturally increase in size even with a simple program while running. Since it has to make classes for minor events that you don't even see mostly.

You can always call System.gc() if you want to force java to garbage collect and reduce the memory usage manually, but you should only use it very sparingly.

Bullring answered 3/10, 2012 at 15:4 Comment(1)
System.gc() does not force a garbage collection - cf javadoc: "Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse."Corrigible

© 2022 - 2025 — McMap. All rights reserved.