服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

提高java程序内存效率


  我们都知道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

扫描关注微信公众号