-2

I need Java Swing Free Memory component (similar to that in Eclipse IDE). Preferably free (and open source). Thank you in advance.

1
  • Just updated my answer in response to your comment
    – VonC
    Aug 31, 2009 at 10:25

3 Answers 3

5

mynameisfred refines its question:

No, I didn't mean MAT.
What I meant was a simple memory indicator you can see on the MAT screenshot in the status bar.

You can display it with:

Preferences - General - Show Heap Status checkbox

(since eclipse3.2, it is no longer displayed by default)

From the blog entry "Eclipse Tweaks: Monitor and run garbage collection on your Eclipse memory heap":

alt text http://www.iheartair.com/samples/images/ShowHeapStatus.png

Note: a much comprehensive solution is the eclipse MAT (Memory Analyzer)?

alt text

A good swing-based Java alternative would be:

VisualVM which can also be used to browse head dump.

alt text

1
  • No, I didn't mean MAT. What I meant was a simple memory indicator you can see on the MAT screenshot in the status bar. Aug 31, 2009 at 10:04
2

If you just need to see memory usage of your application (heap, permanent generation, etc), but without the details of a profiler, check out JConsole. It's bundled with JDK 1.5 and higher.

1

I came across this as yet unanswered question (the two existing answers do not provide what the OP asked for at all) when I searched the web for a ready-made Swing component showing the JVM memory state, just what the OP also needed.

I didn't find anything, so I knocked up a very simple Swing component based on JProgressBar. Including a revolutionary double-click-to-Garbage-Collect feature. And the same texts as the Eclipse component uses. But with correct SI units.

Making it more flexible, with proper i18n etc. is left as an exercise for the reader.

Here's the code:

/*
 * (c) hubersn Software
 * www.hubersn.com
 * 
 * Use wherever you like, change whatever you want. It's free!
 */
package com.hubersn.playground.swing;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class MemoryPanelProgressBar extends JPanel {

  private final JProgressBar progressBar = new JProgressBar();

  public MemoryPanelProgressBar() {
    super(new FlowLayout());
    this.progressBar.setStringPainted(true);
    this.progressBar.setString("");
    this.progressBar.setMinimum(0);
    this.progressBar.setMaximum(100);
    this.progressBar.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(final MouseEvent ev) {
        if (ev.getClickCount() == 2) {
          System.gc();
          update();
        }
      }
    });
    add(this.progressBar);
    Timer t = new Timer(1000, new ActionListener() {
      @Override
      public void actionPerformed(final ActionEvent e) {
        update();
      }
    });
    t.start();
    update();
  }

  private void update() {
    Runtime jvmRuntime = Runtime.getRuntime();
    long totalMemory = jvmRuntime.totalMemory();
    long maxMemory = jvmRuntime.maxMemory();
    long usedMemory = totalMemory - jvmRuntime.freeMemory();
    long totalMemoryInMebibytes = totalMemory / (1024 * 1024);
    long maxMemoryInMebibytes = maxMemory / (1024 * 1024);
    long usedMemoryInMebibytes = usedMemory / (1024 * 1024);
    int usedPct = (int) ((100 * usedMemory) / totalMemory);
    String textToShow = usedMemoryInMebibytes + "MiB of " + totalMemoryInMebibytes + "MiB";
    String toolTipToShow = "Heap size: " + usedMemoryInMebibytes + "MiB of total: " + totalMemoryInMebibytes + "MiB max: "
        + maxMemoryInMebibytes + "MiB";
    this.progressBar.setValue(usedPct);
    this.progressBar.setString(textToShow);
    this.progressBar.setToolTipText(toolTipToShow);
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.