001package jmri.jmrit; 002 003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 004import java.awt.Container; 005import java.awt.GridLayout; 006import java.awt.event.ActionEvent; 007import java.awt.event.ActionListener; 008import javax.swing.AbstractAction; 009import javax.swing.JButton; 010import javax.swing.JLabel; 011import javax.swing.JTextField; 012import jmri.InstanceManager; 013import jmri.jmrit.decoderdefn.DecoderIndexFile; 014import jmri.jmrit.roster.Roster; 015import jmri.util.JmriJFrame; 016 017/** 018 * Display memory usage on request 019 * 020 * @author Bob Jacobsen Copyright (C) 2001, 2008, 2012 021 */ 022public class MemoryFrameAction extends AbstractAction { 023 024 public MemoryFrameAction(String s) { 025 super(s); 026 } 027 028 public MemoryFrameAction() { 029 this(Bundle.getMessage("Memory_Usage_Monitor")); 030 } 031 032 JTextField used1 = new JTextField(15); 033 JTextField used2 = new JTextField(15); 034 JTextField used3 = new JTextField(15); 035 036 JTextField free1 = new JTextField(15); 037 JTextField free2 = new JTextField(15); 038 JTextField free3 = new JTextField(15); 039 040 JTextField total1 = new JTextField(15); 041 JTextField total2 = new JTextField(15); 042 JTextField total3 = new JTextField(15); 043 044 JButton updateButton = new JButton(Bundle.getMessage("Update")); 045 JButton gcButton = new JButton(Bundle.getMessage("Collect_Memory")); 046 JButton testButton = new JButton(Bundle.getMessage("Test")); 047 048 java.text.NumberFormat nf; 049 050 @Override 051 public void actionPerformed(ActionEvent e) { 052 053 nf = java.text.NumberFormat.getInstance(); 054 nf.setMinimumFractionDigits(3); 055 nf.setMaximumFractionDigits(3); 056 nf.setGroupingUsed(false); 057 058 JmriJFrame f = new JmriJFrame(Bundle.getMessage("Memory_Usage_Monitor")); 059 060 Container p = f.getContentPane(); 061 p.setLayout(new GridLayout(5, 3)); 062 063 p.add(new JLabel(Bundle.getMessage("used_(MB)"))); 064 p.add(new JLabel(Bundle.getMessage("free_(MB)"))); 065 p.add(new JLabel(Bundle.getMessage("total_(MB,_of_") + " " + nf.format(Runtime.getRuntime().maxMemory() / (1024. * 1024.)) + "MB)")); 066 067 p.add(used3); 068 p.add(free3); 069 p.add(total3); 070 071 p.add(used2); 072 p.add(free2); 073 p.add(total2); 074 075 p.add(used1); 076 p.add(free1); 077 p.add(total1); 078 079 p.add(updateButton); 080 p.add(gcButton); 081 p.add(testButton); 082 083 updateButton.addActionListener(new ActionListener() { 084 @Override 085 public void actionPerformed(ActionEvent event) { 086 updateDisplay(); 087 } 088 }); 089 gcButton.addActionListener(new ActionListener() { 090 @SuppressFBWarnings(value = "DM_GC") // Garbage collection OK here 091 @Override 092 public void actionPerformed(ActionEvent event) { 093 Runtime.getRuntime().gc(); 094 updateDisplay(); 095 } 096 }); 097 testButton.addActionListener(new ActionListener() { 098 @Override 099 public void actionPerformed(ActionEvent event) { 100 Roster.getDefault(); 101 InstanceManager.getDefault(DecoderIndexFile.class); 102 updateDisplay(); 103 } 104 }); 105 106 f.addHelpMenu("package.jmri.jmrit.MemoryFrameAction", true); 107 108 f.pack(); 109 f.setVisible(true); 110 } 111 112 void updateDisplay() { 113 used3.setText(used2.getText()); 114 free3.setText(free2.getText()); 115 total3.setText(total2.getText()); 116 117 used2.setText(used1.getText()); 118 free2.setText(free1.getText()); 119 total2.setText(total1.getText()); 120 121 double free = Runtime.getRuntime().freeMemory() / (1024. * 1024.); 122 double total = Runtime.getRuntime().totalMemory() / (1024. * 1024.); 123 used1.setText(nf.format(total - free)); 124 free1.setText(nf.format(free)); 125 total1.setText(nf.format(total)); 126 } 127 128}