Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing memory usage

There are plenty of questions on this issue, but no answer satisfied me; I'm coding a simple GUI window using Swing. Currently it only contains 4 buttons and one of them is opening a file chooser.

When I use VisualVM Monitoring tool to look at memory usage, the result is shown below:

memory usage http://img17.imageshack.us/img17/3589/8txc.png

The first pike appeared when I clicked the button, then I did nothing else.

Is this normal for an idle application to consume 10M/min (when it is not doing anything?)

Since I have to do a quizz-like applet with an image, should I use to call System.gc() each time I switch to the next question to avoid (potentially huge?) memory usage?

like image 534
bagage Avatar asked Jan 09 '14 15:01

bagage


People also ask

How do I see Java memory usage?

Using VisualVM (jvisualvm) jvisualvm is a tool to analyse the runtime behavior of your Java application. It allows you to trace a running Java program and see its the memory and CPU consumption. You can also use it to create a memory heap dump to analyze the objects in the heap.

How do I reduce Java memory usage?

Set the Heap Size If you reduce the Java heap size by a certain amount you will reduce the memory footprint of the Java process by the same amount. You can however not reduce the Java heap size infinitely. The heap must be at least large enough for all objects that are alive at the same time.

How much memory does a Java thread use?

Be mindful of thread use and stack size. The default option -Xss512k means that each thread will use 512kb of memory. The JVM default without this option is 1MB.

Does Java use a lot of memory?

Java is consistently listed as using 700MB of real memory.


1 Answers

I could recreate what you saw with a simple test program, but if you let the monitor track a little longer...

enter image description here

CPU was at 0% the whole time (just an open JFileChooser sitting there). The memory fluctuations are curious, but I think the JVM must be doing all kinds of background management and maintenance tasks. That work will use memory which eventually builds up. And periodically it cleans up after itself.

Another thing to consider is that is this memory usage within the already allocated heap. Your program is actually using a consistent amount of real memory the entire time, as you can see from the flat orange line.

like image 188
martinez314 Avatar answered Sep 29 '22 08:09

martinez314