国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

提高Java程序內存效率

2019-11-18 13:11:24
字體:
來源:轉載
供稿:網友

  我們都知道java程序之所以被廣大程序員青睞,很大的一個原因是因為Java有GC(垃圾收集),不用程序員花很大的精力來解決內存釋放和泄漏問題。而這些問題總是C/C++程序員需要花很大精力來認真地面對的。
  
  問題總是雙面的,GC給我們帶來了很大的快樂,釋放了程序員很多的精力和時間,但是在某些時候也會給我們帶來一些小小的麻煩。Java里的Object并非交給GC去釋放就可高枕無憂了,下面從JDK1.4的Demo中Java2D的Memory Monitor說起。
  
  先看看Memory Monitor單獨運行的效果。
  
  可以看到因為有個While循環,GC釋放內存有一定的時間,在這個時間中間,內存消耗的很厲害。峰值達到923K。程序代碼的如下:
  
  //SysGcMain.java Created on 9:15:59
  
  package com.gx2.system;
  
  /**
  
  * @author Frank Gao @version 1.00
  
  * Copy right by GX2 Studio 2003
  
  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
  
  *
  
  * Redistribution and use in source……以后省略
  
  */
  
  /*
  
  * @(#)MemoryMonitor.java  1.32 03/01/23
  
  */
  
  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;
  
  /**
  
  * Tracks Memory allocated & used, displayed in graph form.
  
  */
  
  public class SysGcMain extends JPanel {
  
  static JCheckBox dateStampCB = new JCheckBox("Output Date Stamp");
  
  public Surface surf;
  
  JPanel controls;
  
  boolean doControls;
  
  JTextField tf;
  
  public SysGcMain() {
  
  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.black);
  
  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.black);
  
  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;
  
  private int w, h;
  
  private BufferedImage bimg;
  
  private Graphics2D big;
  
  private Font font = new Font("Times New Roman", Font.PLAIN, 11);
  
  private Runtime r = Runtime.getRuntime();
  
  private int columnInc;
  
  private int pts[];
  
  private int ptNum;
  
  private int ascent, descent;
  
  private float freeMemory, totalMemory;
  
  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();
  
  }
  
  });
  
  }
  
  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);
  
  float freeMemory = (float) r.freeMemory();
  
  float totalMemory = (float) r.totalMemory();
  
  // .. Draw allocated and used strings ..
  
  big.setColor(Color.green);
  
  big.drawString(String.valueOf((int) totalMemory/1024) + "K allocated", 4.0f, (float) ascent+0.5f);
  
  usedStr = String.valueOf(((int) (totalMemory - freeMemory))/1024)
  
  + "K used";
  
  big.drawString(usedStr, 4, h-descent);
  
  // Calculate remaining size
  
  float ssH = ascent + descent;
  
  float remainingHeight = (float) (h - (ssH*2) - 0.5f);
  
  float blockHeight = remainingHeight/10;
  
  float blockWidth = 20.0f;
  
  float remainingWidth = (float) (w - blockWidth - 10);
  
  // .. Memory Free ..
  
  big.setColor(mfColor);
  
  int MemUsage = (int) ((freeMemory / totalMemory) * 10);
  
  int i = 0;
  
  for ( ; i < MemUsage ; i++) {
  
  mfRect.setRect(5,(float) ssH+i*blockHeight,
  
  blockWidth,(float) blockHeight-1);
  
  big.fill(mfRect);
  
  }
  
  // .. Memory Used ..
  
  big.setColor(Color.green);
  
  for ( ; i < 10; i++) {
  
  muRect.setRect(5,(float) ssH+i*blockHeight,
  
  blockWidth,(float) blockHeight-1);
  
  big.fill(muRect);
  
  }
  
  // .. Draw History Graph ..
  
  big.setColor(graphColor);
  
  int graphX = 30;
  
  int graphY = (int) ssH;
  
  int graphW = w - graphX - 5;
  
  int graphH = (int) remainingHeight;
  
  graphOutlineRect.setRect(graphX, graphY, graphW, graphH);
  
  big.draw(graphOutlineRect);
  
  int graphRow = graphH/10;
  
  // .. Draw row ..
  
  for (int j = graphY; j <= graphH+graphY; j += graphRow) {
  
  graphLine.setLine(graphX,j,graphX+graphW,j);
  
  big.draw(graphLine);
  
  }
  
  // .. Draw animated column movement ..
  
  int graphColumn = graphW/15;
  
  if (columnInc == 0) {
  
  columnInc = graphColumn;
  
  }
  
  for (int j = graphX+columnInc; j < graphW+graphX; j+=graphColumn) {
  
  graphLine.setLine(j,graphY,j,graphY+graphH);
  
  big.draw(graphLine);
  
  }
  
  --columnInc;
  
  if (pts == null) {
  
  pts = new int[graphW];
  
  ptNum = 0;
  
  } else if (pts.length != graphW) {
  
  int tmp[] = null;
  
  if (ptNum < graphW) {
  
  tmp = new int[pt

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 呼伦贝尔市| 云阳县| 平定县| 四平市| 黄浦区| 石门县| 彰武县| 甘肃省| 高碑店市| 樟树市| 张掖市| 临泽县| 彝良县| 封开县| 宁德市| 化德县| 景谷| 获嘉县| 武邑县| 光泽县| 桐梓县| 茌平县| 新源县| 夏津县| 安陆市| 河曲县| 卢氏县| 息烽县| 手游| 海淀区| 东兴市| 东方市| 邵阳市| 阿坝县| 阿勒泰市| 白城市| 金寨县| 刚察县| 和林格尔县| 将乐县| 水富县|