首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 程式記憶體監視器實作:基於 Swing 的即時視覺化監控工具完整代碼

     編輯:Java 相關知識

提供一套開箱即用的 Java 記憶體監視視覺化工具,採用 Swing 構建 GUI,獨立執行緒輪詢 Runtime.getRuntime() 獲取堆記憶體使用狀況,以進度條與數值雙重呈現,適合開發階段即時觀察記憶體趨勢與 GC 效果。

核心架構三類別

  • JMemoryDemo:程式入口,建立主視窗並置中顯示
  • MainFrame:主視窗,包含垂直進度條、狀態標籤、標題標籤
  • ProgressThread:監控執行緒,每秒更新記憶體統計並刷新 UI

關鍵代碼實作

主程式入口


public class JMemoryDemo {
    public static void main(String[] args) { new JMemoryDemo(); }
    public JMemoryDemo() {
        MainFrame frame = new MainFrame();
        frame.setVisible(true);
        // 視窗置中邏輯...
    }
}

主視窗構建


public class MainFrame extends JFrame {
    private JProgressBar jProgressBar1 = new JProgressBar();
    private JLabel jLabel1 = new JLabel("Memory Monitor");
    private JLabel jLabel2 = new JLabel();
    public MainFrame() {
        setTitle("JMemoryDemo");
        setSize(304, 215);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jProgressBar1.setOrientation(JProgressBar.VERTICAL);
        jProgressBar1.setStringPainted(true);
        jProgressBar1.setFont(new Font("Dialog", Font.PLAIN, 14));
        // 佈局:北標題、中進度條、南狀態
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(jLabel1, BorderLayout.NORTH);
        getContentPane().add(jProgressBar1, BorderLayout.CENTER);
        getContentPane().add(jLabel2, BorderLayout.SOUTH);
        new ProgressThread(jProgressBar1, jLabel2).start();
    }
    protected void processWindowEvent(WindowEvent e) {
        if (e.getID() == WindowEvent.WINDOW_CLOSING) System.exit(0);
        super.processWindowEvent(e);
    }
}

監控執行緒核心邏輯


public class ProgressThread extends Thread {
    private JProgressBar pbar; private JLabel label;
    public ProgressThread(JProgressBar pbar, JLabel label) { this.pbar = pbar; this.label = label; }
    public void run() {
        pbar.setMinimum(0); pbar.setMaximum(100);
        while (true) {
            long total = Runtime.getRuntime().totalMemory() / 1024; // KB
            long free = Runtime.getRuntime().freeMemory() / 1024;
            int usedPercent = (int) ((total - free) * 100 / total);
            label.setText(String.format("Free Memory: %dK  Total Allocated: %dK", free, total));
            pbar.setValue(100 - usedPercent); // 顯示剩餘百分比
            pbar.setString("Free Memory " + (100 - usedPercent) + "%");
            try { sleep(1000); } catch (InterruptedException ignored) {}
        }
    }
}

核心 API 解析

關鍵洞察:totalMemory() 隨 GC 後釋放空間而可能縮減,maxMemory() 才是真正上限。本工具展示的是「已分配堆中的使用率」,而非「最大堆使用率」。

擴展建議

  1. 加入 maxMemory() 顯示,提供雙軸視角(已分配使用率 / 最大使用率)。
  2. 整合 MemoryPoolMXBean 監控新生代/老年代/元空間分代詳情。
  3. 支援 JMX 遠程連接,監控生產環境 JVM。
  4. 加入 GC 事件聽取器(GarbageCollectorMXBean)標記 GC 發生時刻。
  5. 導出 CSV 歷史數據供事後分析。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
方法含義單位
Runtime.getRuntime().totalMemory()JVM 當前向作業系統申請的總堆記憶體Bytes
Runtime.getRuntime().freeMemory()當前堆中可用空間Bytes
Runtime.getRuntime().maxMemory()JVM 最大可使用堆記憶體(-Xmx)Bytes