|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
windows系统样,他们做了什么事或者留了一些后门程序,谁都不知道,二,java开发是跨平台,任何系统上都可以运行,对于保密型系统和大型系统开发这是必要的程序我们都晓得Java程序之以是被宽大程序员喜爱,很年夜的一个缘故原由是由于Java有GC(渣滓搜集),不必程序员花很年夜的精神来办理内存开释和泄露成绩。而这些成绩老是C/C++程序员必要花很年夜精神来仔细空中对的。
成绩老是双面的,GC给我们带来了很年夜的康乐,开释了程序员良多的精神和工夫,可是在某些时分也会给我们带来一些小小的贫苦。Java里的Object并不是交给GC往开释便可万事大吉了,上面从JDK1.4的Demo中Java2D的MemoryMonitor提及。
先看看MemoryMonitor独自运转的效果。
能够看到由于有个While轮回,GC开释内存有必定的工夫,在这个工夫两头,内存损耗的很凶猛。峰值到达923K。程序代码的以下:
//SysGcMain.javaCreatedon9:15:59
packagecom.gx2.system;
/**
*@authorFrankGao@version1.00
*CopyrightbyGX2Studio2003
*Copyright(c)2003SunMicrosystems,Inc.AllRightsReserved.
*
*Redistributionanduseinsource……今后省略
*/
/*
*@(#)MemoryMonitor.java1.3203/01/23
*/
importjava.awt.*;
importjava.awt.event.*;
importjava.awt.image.BufferedImage;
importjava.awt.geom.Line2D;
importjava.awt.geom.Rectangle2D;
importjava.util.Date;
importjavax.swing.*;
importjavax.swing.border.EtchedBorder;
importjavax.swing.border.TitledBorder;
/**
*TracksMemoryallocated&used,displayedingraphform.
*/
publicclassSysGcMainextendsJPanel{
staticJCheckBoxdateStampCB=newJCheckBox("OutputDateStamp");
publicSurfacesurf;
JPanelcontrols;
booleandoControls;
JTextFieldtf;
publicSysGcMain(){
setLayout(newBorderLayout());
setBorder(newTitledBorder(newEtchedBorder(),"MemoryMonitor"));
add(surf=newSurface());
controls=newJPanel();
controls.setPreferredSize(newDimension(135,80));
Fontfont=newFont("serif",Font.PLAIN,10);
JLabellabel=newJLabel("SampleRate");
label.setFont(font);
label.setForeground(Color.black);
controls.add(label);
tf=newJTextField("1000");
tf.setPreferredSize(newDimension(45,20));
controls.add(tf);
controls.add(label=newJLabel("ms"));
label.setFont(font);
label.setForeground(Color.black);
controls.add(dateStampCB);
dateStampCB.setFont(font);
addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
removeAll();
if((doControls=!doControls)){
surf.stop();
add(controls);
}else{
try{
surf.sleepAmount=Long.parseLong(tf.getText().trim());
}catch(Exceptionex){}
surf.start();
add(surf);
}
validate();
repaint();
}
});
}
publicclassSurfaceextendsJPanelimplementsRunnable{
publicThreadthread;
publiclongsleepAmount=1000;
privateintw,h;
privateBufferedImagebimg;
privateGraphics2Dbig;
privateFontfont=newFont("TimesNewRoman",Font.PLAIN,11);
privateRuntimer=Runtime.getRuntime();
privateintcolumnInc;
privateintpts[];
privateintptNum;
privateintascent,descent;
privatefloatfreeMemory,totalMemory;
privateRectanglegraphOutlineRect=newRectangle();
privateRectangle2DmfRect=newRectangle2D.Float();
privateRectangle2DmuRect=newRectangle2D.Float();
privateLine2DgraphLine=newLine2D.Float();
privateColorgraphColor=newColor(46,139,87);
privateColormfColor=newColor(0,100,0);
privateStringusedStr;
publicSurface(){
setBackground(Color.black);
addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
if(thread==null)start();elsestop();
}
});
}
publicDimensiongetMinimumSize(){
returngetPreferredSize();
}
publicDimensiongetMaximumSize(){
returngetPreferredSize();
}
publicDimensiongetPreferredSize(){
returnnewDimension(135,80);
}
publicvoidpaint(Graphicsg){
if(big==null){
return;
}
big.setBackground(getBackground());
big.clearRect(0,0,w,h);
floatfreeMemory=(float)r.freeMemory();
floattotalMemory=(float)r.totalMemory();
//..Drawallocatedandusedstrings..
big.setColor(Color.green);
big.drawString(String.valueOf((int)totalMemory/1024)+"Kallocated",4.0f,(float)ascent+0.5f);
usedStr=String.valueOf(((int)(totalMemory-freeMemory))/1024)
+"Kused";
big.drawString(usedStr,4,h-descent);
//Calculateremainingsize
floatssH=ascent+descent;
floatremainingHeight=(float)(h-(ssH*2)-0.5f);
floatblockHeight=remainingHeight/10;
floatblockWidth=20.0f;
floatremainingWidth=(float)(w-blockWidth-10);
//..MemoryFree..
big.setColor(mfColor);
intMemUsage=(int)((freeMemory/totalMemory)*10);
inti=0;
for(;i<MemUsage;i++){
mfRect.setRect(5,(float)ssH+i*blockHeight,
blockWidth,(float)blockHeight-1);
big.fill(mfRect);
}
//..MemoryUsed..
big.setColor(Color.green);
for(;i<10;i++){
muRect.setRect(5,(float)ssH+i*blockHeight,
blockWidth,(float)blockHeight-1);
big.fill(muRect);
}
//..DrawHistoryGraph..
big.setColor(graphColor);
intgraphX=30;
intgraphY=(int)ssH;
intgraphW=w-graphX-5;
intgraphH=(int)remainingHeight;
graphOutlineRect.setRect(graphX,graphY,graphW,graphH);
big.draw(graphOutlineRect);
intgraphRow=graphH/10;
//..Drawrow..
for(intj=graphY;j<=graphH+graphY;j+=graphRow){
graphLine.setLine(graphX,j,graphX+graphW,j);
big.draw(graphLine);
}
//..Drawanimatedcolumnmovement..
intgraphColumn=graphW/15;
if(columnInc==0){
columnInc=graphColumn;
}
for(intj=graphX+columnInc;j<graphW+graphX;j+=graphColumn){
graphLine.setLine(j,graphY,j,graphY+graphH);
big.draw(graphLine);
}
--columnInc;
if(pts==null){
pts=newint[graphW];
ptNum=0;
}elseif(pts.length!=graphW){
inttmp[]=null;
if(ptNum<graphW){
tmp=newint[ptNum];
System.arraycopy(pts,0,tmp,0,tmp.length);
}else{
tmp=newint[graphW];
System.arraycopy(pts,pts.length-tmp.length,tmp,0,tmp.length);
ptNum=tmp.length-2;
}
pts=newint[graphW];
System.arraycopy(tmp,0,pts,0,tmp.length);
}else{
big.setColor(Color.yellow);
pts[ptNum]=(int)(graphY+graphH*(freeMemory/totalMemory));
for(intj=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){
//throwoutoldestpoint
for(intj=1;j<ptNum;j++){
pts[j-1]=pts[j];
}
--ptNum;
}else{
ptNum++;
}
}
g.drawImage(bimg,0,0,this);
}
publicvoidstart(){
thread=newThread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.setName("MemoryMonitor");
thread.start();
}
publicsynchronizedvoidstop(){
thread=null;
notify();
}
publicvoidrun(){
Threadme=Thread.currentThread();
while(thread==me&&!isShowing()||getSize().width==0){
try{
thread.sleep(500);
}catch(InterruptedExceptione){return;}
}
while(thread==me&&isShowing()){
Dimensiond=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);
FontMetricsfm=big.getFontMetrics(font);
ascent=(int)fm.getAscent();
descent=(int)fm.getDescent();
}
repaint();
try{
thread.sleep(sleepAmount);
}catch(InterruptedExceptione){break;}
if(SysGcMain.dateStampCB.isSelected()){
System.out.println(newDate().toString()+""+usedStr);
}
//addby健忘幻想2004-11-1
//不强迫GC的话,由于while轮回,内存会损耗的很凶猛
//加高低面两句,能够包管内存损耗安稳
Runtime.getRuntime().freeMemory();
Runtime.getRuntime().gc();
}
thread=null;
}
}
publicstaticvoidmain(Strings[]){
finalSysGcMaindemo=newSysGcMain();
WindowListenerl=newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){System.exit(0);}
publicvoidwindowDeiconified(WindowEvente){demo.surf.start();}
publicvoidwindowIconified(WindowEvente){demo.surf.stop();}
};
JButtonbutton=newJButton("ForceGC");
button.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevent){
Runtime.getRuntime().freeMemory();
Runtime.getRuntime().gc();
}
});
JFramef=newJFrame("FrankGao-MemoryMonitor");
f.addWindowListener(l);
f.getContentPane().add("Center",demo);
f.getContentPane().add(button,BorderLayout.SOUTH);
f.pack();
f.setSize(newDimension(200,200));
f.setVisible(true);
demo.surf.start();
}
}
在while轮回中到场强迫gc,内存峰值失掉了很好的把持保持程度。
Runtime.getRuntime().freeMemory();
Runtime.getRuntime().gc();
由此能够思索在,程序while等对照好内存的中央接纳上述办法,下降内存峰值,进步内存效力。
你说是sun公司对她研究的透还是微软?针对自己工具开发的.net性能上肯定会站上风的。 |
|