📄 memorymonitor.java
字号:
/* * @(#)MemoryMonitor.java 1.3 05/11/17 * * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. *//* * @(#)MemoryMonitor.java 1.3 05/11/17 */import java.awt.*;import java.awt.event.*;import java.awt.image.BufferedImage;import java.awt.geom.Line2D;import java.awt.geom.Rectangle2D;import java.util.Date;import javax.swing.*;import javax.swing.border.EtchedBorder;import javax.swing.border.TitledBorder;import java.lang.management.*;import java.util.*;/** * Demo code which plots the memory usage by all memory pools. * The memory usage is sampled at some time interval using * java.lang.management API. This demo code is modified based * java2d MemoryMonitor demo. */public class MemoryMonitor extends JPanel { static JCheckBox dateStampCB = new JCheckBox("Output Date Stamp"); public Surface surf; JPanel controls; boolean doControls; JTextField tf; // Get memory pools. static java.util.List<MemoryPoolMXBean> mpools = ManagementFactory.getMemoryPoolMXBeans(); // Total number of memory pools. static int numPools = mpools.size(); public MemoryMonitor() { setLayout(new BorderLayout()); setBorder(new TitledBorder(new EtchedBorder(), "Memory Monitor")); add(surf = new Surface()); controls = new JPanel(); controls.setPreferredSize(new Dimension(135,80)); Font font = new Font("serif", Font.PLAIN, 10); JLabel label = new JLabel("Sample Rate"); label.setFont(font); label.setForeground(Color.red); controls.add(label); tf = new JTextField("1000"); tf.setPreferredSize(new Dimension(45,20)); controls.add(tf); controls.add(label = new JLabel("ms")); label.setFont(font); label.setForeground(Color.red); controls.add(dateStampCB); dateStampCB.setFont(font); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { removeAll(); if ((doControls = !doControls)) { surf.stop(); add(controls); } else { try { surf.sleepAmount = Long.parseLong(tf.getText().trim()); } catch (Exception ex) {} surf.start(); add(surf); } validate(); repaint(); } }); } public class Surface extends JPanel implements Runnable { public Thread thread; public long sleepAmount = 1000; public int usageHistCount = 20000; private int w, h; private BufferedImage bimg; private Graphics2D big; private Font font = new Font("Times New Roman", Font.PLAIN, 11); private int columnInc; private float usedMem[][]; private int ptNum[]; private int ascent, descent; private Rectangle graphOutlineRect = new Rectangle(); private Rectangle2D mfRect = new Rectangle2D.Float(); private Rectangle2D muRect = new Rectangle2D.Float(); private Line2D graphLine = new Line2D.Float(); private Color graphColor = new Color(46, 139, 87); private Color mfColor = new Color(0, 100, 0); private String usedStr; public Surface() { setBackground(Color.black); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (thread == null) start(); else stop(); } }); int i = 0; usedMem = new float[numPools][]; ptNum = new int[numPools]; } public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getMaximumSize() { return getPreferredSize(); } public Dimension getPreferredSize() { return new Dimension(135,80); } public void paint(Graphics g) { if (big == null) { return; } big.setBackground(getBackground()); big.clearRect(0,0,w,h); h = h / ((numPools + numPools%2) / 2); w = w / 2; int k=0; // index of memory pool. for (int i=0; i < 2;i++) { for (int j=0; j < (numPools + numPools%2)/ 2; j++) { plotMemoryUsage(w*i,h*j,w,h,k); if (++k >= numPools) { i = 3; j = (numPools + numPools%2)/ 2; break; } } } g.drawImage(bimg, 0, 0, this); } public void plotMemoryUsage(int x1, int y1, int x2, int y2, int npool) { MemoryPoolMXBean mp = mpools.get(npool); float usedMemory = mp.getUsage().getUsed(); float totalMemory = mp.getUsage().getMax(); // .. Draw allocated and used strings .. big.setColor(Color.green); // Print Max memory allocated for this memory pool. big.drawString(String.valueOf((int)totalMemory/1024) + "K Max ", x1+4.0f, (float) y1 + ascent+0.5f); big.setColor(Color.yellow); // Print the memory pool name. big.drawString(mp.getName(), x1+x2/2, (float) y1 + ascent+0.5f); // Print the memory used by this memory pool. usedStr = String.valueOf((int)usedMemory/1024) + "K used"; big.setColor(Color.green); big.drawString(usedStr, x1+4, y1+y2-descent); // Calculate remaining size float ssH = ascent + descent; float remainingHeight = (float) (y2 - (ssH*2) - 0.5f); float blockHeight = remainingHeight/10; float blockWidth = 20.0f; float remainingWidth = (float) (x2 - blockWidth - 10); // .. Memory Free .. big.setColor(mfColor); int MemUsage = (int) (((totalMemory - usedMemory) / totalMemory) * 10); int i = 0; for ( ; i < MemUsage ; i++) { mfRect.setRect(x1+5,(float) y1+ssH+i*blockHeight, blockWidth,(float) blockHeight-1); big.fill(mfRect); } // .. Memory Used .. big.setColor(Color.green); for ( ; i < 10; i++) { muRect.setRect(x1+5,(float) y1 + ssH+i*blockHeight, blockWidth,(float) blockHeight-1); big.fill(muRect); } // .. Draw History Graph .. if (remainingWidth <= 30) remainingWidth = (float)30; if (remainingHeight <= ssH) remainingHeight = (float)ssH; big.setColor(graphColor);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -