import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.management.ManagementFactory; import java.lang.management.MemoryPoolMXBean; import java.lang.management.MemoryUsage; import java.lang.management.MemoryType; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.border.EmptyBorder; public class MemoryPoolMXBeanTest { private List heapMemoryPools; private List nonHeapMemoryPools; protected JFrame frame; protected JPanel panel; private JTabbedPane tabbedPane; private Map panes; private DecimalFormat format; private final static double TRANS_B_TO_M = 1024.0 * 1024.0; private final static double TRANS_B_TO_K = 1024.0; public MemoryPoolMXBeanTest() { format = new DecimalFormat("0.00"); panes = new HashMap(); initView(); heapMemoryPools = new ArrayList(); nonHeapMemoryPools = new ArrayList(); List mbeans = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean mbean: mbeans) { if (mbean.getType() == MemoryType.HEAP) { heapMemoryPools.add(mbean); } else { nonHeapMemoryPools.add(mbean); } } Comparator comparator = new Comparator() { public int compare(MemoryPoolMXBean mbean1, MemoryPoolMXBean mbean2) { return mbean1.getName().compareTo(mbean2.getName()); } }; Collections.sort(heapMemoryPools, comparator); Collections.sort(nonHeapMemoryPools, comparator); createInfoTab(); } private void initView() { frame = new JFrame("Memory Pool Infomation"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 600); tabbedPane = new JTabbedPane(); frame.getContentPane().add(tabbedPane); panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.RIGHT)); JButton updateButton = new JButton("Update"); updateButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { updateInfoTab(); } }); panel.add(updateButton); frame.getContentPane().add(panel, BorderLayout.NORTH); frame.setVisible(true); } private void createInfoTab() { for (MemoryPoolMXBean mbean: heapMemoryPools) { addInfoTab(mbean); } for (MemoryPoolMXBean mbean: nonHeapMemoryPools) { addInfoTab(mbean); } } private void clearInfoTab() { tabbedPane.removeAll(); } private void addInfoTab(MemoryPoolMXBean mbean) { MemoryPoolPane pane = new MemoryPoolPane(mbean); pane.setBorder(new EmptyBorder(10, 10, 10, 10)); panes.put(mbean.getName(), pane); tabbedPane.add(mbean.getName(), new JScrollPane(pane)); } protected void updateInfoTab() { for (MemoryPoolMXBean mbean: heapMemoryPools) { updateInfo(mbean); } for (MemoryPoolMXBean mbean: nonHeapMemoryPools) { updateInfo(mbean); } } private void updateInfo(MemoryPoolMXBean mbean) { MemoryPoolPane pane = panes.get(mbean.getName()); pane.updateInfo(mbean); } class MemoryPoolPane extends JPanel { JLabel nameLabel; JLabel typeLabel; JLabel validLabel; JLabel memoryManagerNamesLabel; JLabel usageLabel; JLabel usageThresholdLabel; JLabel usageThresholdSupportedLabel; JLabel usageThresholdExceededLabel; JLabel usageThresholdCountLabel; JLabel peakUsageLabel; JLabel collectionUsageLabel; JLabel collectionUsageThresholdLabel; JLabel collectionUsageThresholdSupportedLabel; JLabel collectionUsageThresholdExceededLabel; JLabel collectionUsageThresholdCountLabel; MemoryUsageGraph usageGraph; MemoryUsageGraph peakUsageGraph; MemoryUsageGraph collectionUsageGraph; MemoryPoolPane(MemoryPoolMXBean mbean) { setLayout(new CompactGridLayout(0, 2, 2, 20)); add(new JLabel("Name")); nameLabel = new JLabel(); add(nameLabel); add(new JLabel("Type")); typeLabel = new JLabel(); add(typeLabel); add(new JLabel("Valid")); validLabel = new JLabel(); add(validLabel); add(new JLabel("MemoryManagerNames")); memoryManagerNamesLabel = new JLabel(); add(memoryManagerNamesLabel); add(new JLabel("Usage")); usageLabel = new JLabel(); add(usageLabel); add(Box.createGlue()); usageGraph = new MemoryUsageGraph(); add(usageGraph); add(new JLabel("UsageThresholdSupported")); usageThresholdSupportedLabel = new JLabel(); add(usageThresholdSupportedLabel); add(new JLabel("UsageThreshold")); usageThresholdLabel = new JLabel(); add(usageThresholdLabel); add(new JLabel("UsageThresholdExceeded")); usageThresholdExceededLabel = new JLabel(); add(usageThresholdExceededLabel); add(new JLabel("UsageThresholdCount")); usageThresholdCountLabel = new JLabel(); add(usageThresholdCountLabel); add(new JLabel("PeakUsage")); peakUsageLabel = new JLabel(); add(peakUsageLabel); add(Box.createGlue()); peakUsageGraph = new MemoryUsageGraph(); add(peakUsageGraph); add(new JLabel("CollectionUsage")); collectionUsageLabel = new JLabel(); add(collectionUsageLabel); add(Box.createGlue()); collectionUsageGraph = new MemoryUsageGraph(); add(collectionUsageGraph); add(new JLabel("CollectionUsageThresholdSupported")); collectionUsageThresholdSupportedLabel = new JLabel(); add(collectionUsageThresholdSupportedLabel); add(new JLabel("CollectionUsageThreshold")); collectionUsageThresholdLabel = new JLabel(); add(collectionUsageThresholdLabel); add(new JLabel("CollectionUsageThresholdExceeded")); collectionUsageThresholdExceededLabel = new JLabel(); add(collectionUsageThresholdExceededLabel); add(new JLabel("CollectionUsageThresholdCount")); collectionUsageThresholdCountLabel = new JLabel(); add(collectionUsageThresholdCountLabel); updateInfo(mbean); } public void updateInfo(MemoryPoolMXBean mbean) { String name = ""; String type = ""; String valid = ""; String memoryManagerNames = ""; String usageTxt = ""; String usageThreshold = "-1"; String usageThresholdSupported = ""; String usageThresholdExceeded = "0"; String usageThresholdCount = "0"; String peakUsageTxt = ""; String collectionUsageTxt = ""; String collectionUsageThreshold = "-1"; String collectionUsageThresholdSupported = ""; String collectionUsageThresholdExceeded = "0"; String collectionUsageThresholdCount = "0"; synchronized (mbean) { name = mbean.getName(); type = mbean.getType().toString(); valid = Boolean.toString(mbean.isValid()); StringBuilder builder = new StringBuilder(); builder.append(""); for (Object obj: mbean.getMemoryManagerNames()) { builder.append(obj); builder.append("
"); } memoryManagerNames = builder.toString(); MemoryUsage usage = mbean.getUsage(); usageGraph.setMemoryUsage(usage); usageTxt = createUsageText(usage); usageThresholdSupported = Boolean.toString(mbean.isUsageThresholdSupported()); if (mbean.isUsageThresholdSupported()) { usageThreshold = Long.toString(mbean.getUsageThreshold()); usageThresholdExceeded = Boolean.toString(mbean.isUsageThresholdExceeded()); usageThresholdCount = Long.toString(mbean.getUsageThresholdCount()); } usage = mbean.getPeakUsage(); peakUsageGraph.setMemoryUsage(usage); peakUsageTxt = createUsageText(usage); collectionUsageThresholdSupported = Boolean.toString(mbean.isCollectionUsageThresholdSupported()); usage = mbean.getCollectionUsage(); if (usage != null) { collectionUsageGraph.setMemoryUsage(usage); collectionUsageTxt = createUsageText(usage); } if (mbean.isCollectionUsageThresholdSupported()) { collectionUsageThreshold = Long.toString(mbean.getCollectionUsageThreshold()); collectionUsageThresholdExceeded = Boolean.toString(mbean.isCollectionUsageThresholdExceeded()); collectionUsageThresholdCount = Long.toString(mbean.getCollectionUsageThresholdCount()); } } nameLabel.setText(name); typeLabel.setText(type); validLabel.setText(valid); memoryManagerNamesLabel.setText(memoryManagerNames); usageLabel.setText(usageTxt); usageThresholdSupportedLabel.setText(usageThresholdSupported); usageThresholdLabel.setText(usageThreshold); usageThresholdExceededLabel.setText(usageThresholdExceeded); usageThresholdCountLabel.setText(usageThresholdCount); peakUsageLabel.setText(peakUsageTxt); collectionUsageThresholdSupportedLabel.setText(collectionUsageThresholdSupported); collectionUsageLabel.setText(collectionUsageTxt); collectionUsageThresholdLabel.setText(collectionUsageThreshold); collectionUsageThresholdExceededLabel.setText(collectionUsageThresholdExceeded); collectionUsageThresholdCountLabel.setText(collectionUsageThresholdCount); } } protected String createUsageText(MemoryUsage usage) { StringBuilder builder = new StringBuilder(); builder.append("("); double max = usage.getMax(); if (max > 2000000L) { max /= TRANS_B_TO_M; builder.append(format.format(max)); builder.append("M, "); } else { max /= TRANS_B_TO_K; builder.append(format.format(max)); builder.append("K, "); } double committed = usage.getCommitted(); if (committed > 2000000L) { committed /= TRANS_B_TO_M; builder.append(format.format(committed)); builder.append("M): "); } else { committed /= TRANS_B_TO_K; builder.append(format.format(committed)); builder.append("K): "); } double used = usage.getUsed(); if (used > 2000000L) { used /= TRANS_B_TO_M; builder.append(format.format(used)); builder.append("M"); } else { used /= TRANS_B_TO_K; builder.append(format.format(used)); builder.append("K"); } return builder.toString(); } public static void main(String[] args) { new MemoryPoolMXBeanTest(); } }