View Javadoc

    1   /*
    2    * @(#) MemoryFrame.java Nov 5, 2003
    3    *
    4    * Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628 BX
    5    * Delft, the Netherlands. All rights reserved.
    6    *
    7    * This software is proprietary information of Delft University of Technology
    8    * The code is published under the Lesser General Public License
    9    */
    10
    11  package nl.tudelft.simulation.dsol.gui.windows;
    12
    13  import java.awt.BorderLayout;
    14  import java.awt.event.ActionEvent;
    15  import java.awt.event.ActionListener;
    16  import java.text.NumberFormat;
    17
    18  import javax.swing.BorderFactory;
    19  import javax.swing.JButton;
    20  import javax.swing.JFrame;
    21  import javax.swing.JPanel;
    22  import javax.swing.JTextField;
    23  import javax.swing.WindowConstants;
    24
    25  import nl.tudelft.simulation.dsol.gui.panels.TablePanel;
    26  import nl.tudelft.simulation.logger.Logger;
    27
    28  /***
    29   * @author peter
    30   */
    31  public class MemoryFrame extends JFrame implements ActionListener
    32  {
    33  	/*** freeMem */
    34  	private JTextField freeMemoryField = new JTextField();
    35
    36  	/*** usedMem */
    37  	private JTextField totalMemoryField = new JTextField();
    38
    39  	/*** total Mem */
    40  	private JTextField usedMemoryField = new JTextField();
    41
    42  	/***
    43  	 * constructs a new MemoryFrame
    44  	 *
    45  	 */
    46  	public MemoryFrame()
    47  	{
    48  		super("Memory information");
    49  		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    50  		this.initialize();
    51  		new Worker(this).start();
    52  		this.pack();
    53  		this.setResizable(false);
    54  		this.setVisible(true);
    55  	}
    56
    57  	/***
    58  	 * initializes
    59  	 */
    60  	private void initialize()
    61  	{
    62  		this.freeMemoryField.setEditable(false);
    63  		this.usedMemoryField.setEditable(false);
    64  		this.totalMemoryField.setEditable(false);
    65  		JPanel panel = new JPanel(new BorderLayout());
    66  		TablePanel buttonPanel = new TablePanel(1, 1);
    67  		JButton button = new JButton("Clean garbage now !");
    68  		button.addActionListener(this);
    69  		buttonPanel.setCell(button, 0, 0);
    70  		panel.add(buttonPanel, BorderLayout.SOUTH);
    71  		TablePanel memoryPanel = new TablePanel(3, 2);
    72  		memoryPanel.setBorder(BorderFactory.createTitledBorder("Memory"));
    73  		JTextField freeLabel = new JTextField("Free memory (bytes)");
    74  		freeLabel.setEditable(false);
    75  		JTextField usedLabel = new JTextField("Used memory (bytes)");
    76  		usedLabel.setEditable(false);
    77  		JTextField totalLabel = new JTextField("Total memory (bytes)");
    78  		totalLabel.setEditable(false);
    79  		memoryPanel.setCell(freeLabel, 0, 0);
    80  		memoryPanel.setCell(usedLabel, 1, 0);
    81  		memoryPanel.setCell(totalLabel, 2, 0);
    82  		memoryPanel.setCell(this.freeMemoryField, 0, 1);
    83  		memoryPanel.setCell(this.usedMemoryField, 1, 1);
    84  		memoryPanel.setCell(this.totalMemoryField, 2, 1);
    85  		panel.add(memoryPanel, BorderLayout.CENTER);
    86  		this.setContentPane(panel);
    87  		this.validate();
    88  	}
    89
    90  	/***
    91  	 * @see java.awt.event.ActionListener
    92  	 *      #actionPerformed(java.awt.event.ActionEvent)
    93  	 */
    94  	public void actionPerformed(final ActionEvent event)
    95  	{
    96  		Runtime.getRuntime().gc();
    97  		this.repaint();
    98  	}
    99
    100 	/***
    101 	 * gets freeMemoryField
    102 	 *
    103 	 * @return Returns the freeMemoryField.
    104 	 */
    105 	protected JTextField getFreeMemoryField()
    106 	{
    107 		return this.freeMemoryField;
    108 	}
    109
    110 	/***
    111 	 * gets totalMemoryField
    112 	 *
    113 	 * @return Returns the totalMemoryField.
    114 	 */
    115 	protected JTextField getTotalMemoryField()
    116 	{
    117 		return this.totalMemoryField;
    118 	}
    119
    120 	/***
    121 	 * gets usedMemoryField
    122 	 *
    123 	 * @return Returns the usedMemoryField.
    124 	 */
    125 	protected JTextField getUsedMemoryField()
    126 	{
    127 		return this.usedMemoryField;
    128 	}
    129
    130 	/***
    131 	 * @author peter
    132 	 */
    133 	private class Worker extends Thread
    134 	{
    135 		/*** a formatter */
    136 		private NumberFormat formatter = NumberFormat.getInstance();
    137
    138 		/*** the memoryFrame */
    139 		private MemoryFrame frame = null;
    140
    141 		/***
    142 		 * constructs a new SimulatorRunThread
    143 		 *
    144 		 * @param frame the frame.
    145 		 */
    146 		public Worker(final MemoryFrame frame)
    147 		{
    148 			super();
    149 			this.frame = frame;
    150 			this.formatter.setMaximumFractionDigits(1);
    151 		}
    152
    153 		/***
    154 		 * @see java.lang.Runnable#run()
    155 		 */
    156 		public void run()
    157 		{
    158 			try
    159 			{
    160 				while (true)
    161 				{
    162 					long free = Runtime.getRuntime().freeMemory();
    163 					long total = Runtime.getRuntime().totalMemory();
    164 					long used = total - free;
    165 					this.frame
    166 							.getFreeMemoryField()
    167 							.setText(
    168 									free
    169 											+ "  ("
    170 											+ this.formatter
    171 													.format((free / (double) total) * 100)
    172 											+ "%)");
    173 					this.frame
    174 							.getUsedMemoryField()
    175 							.setText(
    176 									used
    177 											+ "  ("
    178 											+ this.formatter
    179 													.format((used / (double) total) * 100)
    180 											+ "%)");
    181 					this.frame.getTotalMemoryField().setText(
    182 							total + "  (100.0%)");
    183 					MemoryFrame.this.validate();
    184 					MemoryFrame.this.repaint();
    185 					Thread.sleep(3500);
    186 				}
    187 			} catch (Exception exception)
    188 			{
    189 				Logger.warning(this, "run", exception);
    190 			}
    191 		}
    192 	}
    193 }