我们都知道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[ptnum];
system.arraycopy(pts, 0, tmp, 0, tmp.length);
} else {
tmp = new int[graphw];
system.arraycopy(pts, pts.length-tmp.length, tmp, 0, tmp.length);
ptnum = tmp.length – 2;
}
pts = new int[graphw];
system.arraycopy(tmp, 0, pts, 0, tmp.length);
} else {
big.setcolor(color.yellow);
pts[ptnum] = (int)(graphy+graphh*(freememory/totalmemory));
for (int j=graphx+graphw-ptnum, k=0;k < ptnum; k++, j++) {
if (k != 0) {
if (pts[k] != pts[k-1]) {
big.drawline(j-1, pts[k-1], j, pts[k]);
} else {
big.fillrect(j, pts[k], 1, 1);
}
}
}
if (ptnum+2 == pts.length) {
// throw out oldest point
for (int j = 1;j < ptnum; j++) {
pts[j-1] = pts[j];
}
–ptnum;
} else {
ptnum++;
}
}
g.drawimage(bimg, 0, 0, this);
}
public void start() {
thread = new thread(this);
thread.setpriority(thread.min_priority);
thread.setname("memorymonitor");
thread.start();
}
public synchronized void stop() {
thread = null;
notify();
}
public void run() {
thread me = thread.currentthread();
while (thread == me && !isshowing() || getsize().width == 0) {
try {
thread.sleep(500);
} catch (interruptedexception e) { return; }
}
while (thread == me && isshowing()) {
dimension d = getsize();
if (d.width != w || d.height != h) {
w = d.width;
h = d.height;
bimg = (bufferedimage) createimage(w, h);
big = bimg.creategraphics();
big.setfont(font);
fontmetrics fm = big.getfontmetrics(font);
ascent = (int) fm.getascent();
descent = (int) fm.getdescent();
}
repaint();
try {
thread.sleep(sleepamount);
} catch (interruptedexception e) { break; }
if (sysgcmain.datestampcb.isselected()) {
system.out.println(new date().tostring() + " " + usedstr);
}
//add by 忘记理想 2004-11-1
// 不强制gc的话,因为while循环,内存会消耗的很厉害
// 加上下面两句,可以保证内存消耗平稳
runtime.getruntime().freememory();
runtime.getruntime().gc();
}
thread = null;
}
}
public static void main(string s[]) {
final sysgcmain demo = new sysgcmain();
windowlistener l = new windowadapter() {
public void windowclosing(windowevent e) {system.exit(0);}
public void windowdeiconified(windowevent e) { demo.surf.start(); }
public void windowiconified(windowevent e) { demo.surf.stop(); }
};
jbutton button = new jbutton("force gc");
button.addactionlistener(new actionlistener(){
public void actionperformed(actionevent event){
runtime.getruntime().freememory();
runtime.getruntime().gc();
}
});
jframe f = new jframe("frank gao – memorymonitor");
f.addwindowlistener(l);
f.getcontentpane().add("center", demo);
f.getcontentpane().add(button,borderlayout.south);
f.pack();
f.setsize(new dimension(200,200));
f.setvisible(true);
demo.surf.start();
}
}
在while循环中加入强制gc,内存峰值得到了很好的控制维持水平。
runtime.getruntime().freememory();
runtime.getruntime().gc();
由此可以考虑在,程序while等比较好内存的地方采用上述方法,降低内存峰值,提高内存效率。
