Coverage Report - org.argouml.ui.HeapMonitor
 
Classes in this File Line Coverage Branch Coverage Complexity
HeapMonitor
100%
38/38
N/A
1
 
 1  
 /* $Id: HeapMonitor.java 17841 2010-01-12 19:17:52Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2007 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies. This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.ui;
 40  
 
 41  
 import java.awt.Color;
 42  
 import java.awt.Dimension;
 43  
 import java.awt.Graphics;
 44  
 import java.awt.Rectangle;
 45  
 import java.awt.event.ActionEvent;
 46  
 import java.awt.event.ActionListener;
 47  
 import java.text.MessageFormat;
 48  
 
 49  
 import javax.swing.JComponent;
 50  
 import javax.swing.Timer;
 51  
 
 52  
 /**
 53  
  * Swing component to monitor Java heap usage.
 54  
  * 
 55  
  * @author Tom Morris <tfmorris@gmail.com>
 56  
  */
 57  
 public class HeapMonitor extends JComponent implements ActionListener {
 58  
 
 59  
     // % thresholds for bar color changes
 60  
     private static final int ORANGE_THRESHOLD = 70;
 61  
     private static final int RED_THRESHOLD = 90;
 62  
     
 63  900
     private static final Color GREEN = new Color(0, 255, 0);
 64  900
     private static final Color ORANGE  = new Color(255, 190, 125);
 65  900
     private static final Color RED = new Color(255, 70, 70);
 66  
 
 67  
     private static final long M = 1024 * 1024;
 68  
     
 69  
     // Virtual memory (heap) stats
 70  
     private long free;
 71  
     private long total;
 72  
     private long max;
 73  
     private long used;
 74  
     
 75  
     /**
 76  
      * Construct a graphical JVM heap monitor component.
 77  
      */
 78  
     public HeapMonitor() {
 79  900
          super();
 80  900
         Dimension size = new Dimension(200, 20);
 81  900
         setPreferredSize(size);
 82  
 
 83  
         // TODO: Add a button to force garbage collection
 84  
 
 85  900
         updateStats();
 86  
 
 87  900
         Timer timer = new Timer(1000, this);
 88  900
         timer.start();
 89  900
     }
 90  
     
 91  
     public void paint (Graphics g) {        
 92  37094
         Rectangle bounds = getBounds();
 93  37094
         int usedX = (int) (used * bounds.width / total);
 94  37094
         int warnX = ORANGE_THRESHOLD * bounds.width / 100;
 95  37094
         int dangerX = RED_THRESHOLD * bounds.width / 100;
 96  
         
 97  37094
         Color savedColor = g.getColor();
 98  
         
 99  
 //      g.setColor(GREEN);
 100  
         // TODO: We want something minimally distracting here.  Another option
 101  
         // might be just the background color with a solid end line.
 102  37094
         g.setColor(getBackground().darker());
 103  37094
         g.fillRect(0, 0, Math.min(usedX, warnX), bounds.height);
 104  
         
 105  37094
         g.setColor(ORANGE);
 106  37093
         g.fillRect(warnX, 0, 
 107  
                 Math.min(usedX - warnX, dangerX - warnX), 
 108  
                 bounds.height);
 109  
         
 110  37093
         g.setColor(RED);
 111  37093
         g.fillRect(dangerX, 0, 
 112  
                 Math.min(usedX - dangerX, bounds.width - dangerX), 
 113  
                 bounds.height);
 114  
 
 115  37093
         g.setColor(getForeground());
 116  
 
 117  37093
         String s = MessageFormat.format("{0}M used of {1}M total",
 118  
                 new Object[] {(long) (used / M), (long) (total / M) });
 119  37093
         int x = (bounds.width - g.getFontMetrics().stringWidth(s)) / 2;
 120  37093
         int y = (bounds.height + g.getFontMetrics().getHeight()) / 2;
 121  37093
         g.drawString(s, x, y);
 122  
         
 123  37093
         g.setColor(savedColor);
 124  37093
     }
 125  
 
 126  
     /*
 127  
      * Timer action method.  Periodically update our stats and force a repaint.
 128  
      */
 129  
     public void actionPerformed(ActionEvent e) {
 130  40205
         updateStats();
 131  40205
         repaint();
 132  40205
     }
 133  
     
 134  
     private void updateStats() {
 135  41105
         free = Runtime.getRuntime().freeMemory();
 136  41105
         total = Runtime.getRuntime().totalMemory();
 137  41105
         max = Runtime.getRuntime().maxMemory();
 138  41105
         used = total - free;
 139  
 
 140  41105
         String tip = MessageFormat.format(
 141  
                 "Heap use: {0}%  {1}M used of {2}M total.  Max: {3}M", 
 142  
                 new Object[] {used * 100 / total, (long) (used / M),
 143  
                               (long) (total / M), (long) (max / M)
 144  
                 });
 145  41105
         setToolTipText(tip);
 146  41105
     }
 147  
     
 148  
 }