【发布时间】:2010-11-23 16:25:43
【问题描述】:
我需要 Java Swing Free Memory 组件(类似于 Eclipse IDE 中的组件)。最好是免费的(和开源的)。提前谢谢你。
【问题讨论】:
-
刚刚根据您的评论更新了我的答案
标签: java swing components
我需要 Java Swing Free Memory 组件(类似于 Eclipse IDE 中的组件)。最好是免费的(和开源的)。提前谢谢你。
【问题讨论】:
标签: java swing components
mynameisfred 细化了它的问题:
不,我不是说 MAT。
我的意思是一个简单的内存指示器,您可以在状态栏中的 MAT 屏幕截图中看到。
您可以使用以下方式显示它:
Preferences - General - Show Heap Status checkbox
(由于eclipse3.2,默认不再显示)
来自博客条目“Eclipse Tweaks: Monitor and run garbage collection on your Eclipse memory heap”:
注意:一个更全面的解决方案是 eclipse MAT (Memory Analyzer)?
一个好的基于 swing 的 Java 替代方案是:
【讨论】:
如果您只需要查看应用程序的内存使用情况(堆、永久代等),但没有分析器的详细信息,请查看 JConsole。它与 JDK 1.5 及更高版本捆绑在一起。
【讨论】:
当我在网上搜索显示 JVM 内存状态的现成 Swing 组件时,我遇到了这个尚未回答的问题(两个现有答案根本没有提供 OP 所要求的),这正是 OP 也需要。
什么都没找到,就敲了一个非常简单的基于JProgressBar的Swing组件。包括革命性的双击垃圾收集功能。以及与 Eclipse 组件使用的文本相同的文本。但使用正确的 SI 单位。
使用适当的 i18n 等使其更灵活,留给读者作为练习。
代码如下:
/*
* (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);
}
}
【讨论】: