这是一款基于LGame-Simple-0.2.5的小应用,能够针对用户指定的GC日志文件(仅限Sun JRE生成的)进行简单分析。PS:此示例中吾辈对图表的动态绘制也进行了初步尝试(比较痛苦到底是该封装jfreechart之类的图形库好,还是该自己写套小型图形库用的方便……不过那是N往后的计划了……)。
从技术角度来讲,此例并非一个完整的GC日志分析工具,而仅仅是一个用以显示GC日志数据的监听器。它的实现机理堪称简单至极,不过是利用-Xloggc输出JVM的GC记录,再以正则表达式解析到图表中显示罢了(当然,HPjtnue也是这样玩的)。
因此,隐藏在这个简单示例背后,如何更为高效,准确,有价值的分析出必要数据的方式以及方法,才是我们应该思考的问题(只对Java2D感兴趣的就研制表格绘制好了……)。
下载地址(将批处理中的gc日志地址改为需要监听的日志地址即可使用) :http://loon-simple.googlecode.com/files/GCLog.7z
执行部分代码如下(详细代码在jar中):
package org.loon.test; import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import org.loon.framework.game.simple.GameScene; import org.loon.framework.game.simple.core.LSystem; import org.loon.framework.game.simple.core.graphics.Deploy; import org.loon.framework.game.simple.core.graphics.Screen; /** * * Copyright 2008 - 2009 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @project loonframework * @author chenpeng * @email:[email protected] * @version 0.1 */ public class Main extends Screen { private ChartSprite chart; private GC gc; public Main(String fileName) { this.gc = new GCLog(fileName); this.chart = new ChartSprite(getWidth(), getHeight()); this.chart.showLegendOnRight(); this.chart.setXDescription("Run Time"); this.chart.setYDescription("bytes"); this.chart.setShowSmallSquares(6); this.chart.setTitle("GC Line2D Chart"); this.chart.setMaxValue(20000); this.chart.setValsDescription("0,0,0,0,0,0"); this.chart.addComp("0,0,0,0,0,0", "Used"); this.chart.addComp("0,0,0,0,0,0", "Free"); this.chart.addComp("0,0,0,0,0,0", "Total"); this.add(chart); Thread thread = new Thread() { public void run() { while (true) { GCData m = gc.read(); if (m != null) { chart.updateComp(0, m.getUsedMemory()); chart.updateComp(1, m.getFreeMemory()); chart.updateComp(2, m.getTotalMemory()); chart.updateDescription(m.getTime()); } try { Thread.sleep(LSystem.SECOND); } catch (InterruptedException e) { } } } }; thread.start(); } public void draw(Graphics2D g) { } public void leftClick(MouseEvent e) { } public void middleClick(MouseEvent e) { } public void rightClick(MouseEvent e) { } public void onKey(KeyEvent e) { } public void onKeyUp(KeyEvent e) { } public static void main(final String[] args) { if (args.length == 0) { return; } GameScene frame = new GameScene("GC日志监听器", 480, 360); Deploy deploy = frame.getDeploy(); deploy.setScreen(new Main(args[0])); deploy.setShowFPS(false); deploy.setLogo(false); deploy.setFPS(30); deploy.mainLoop(); frame.showFrame(); } }
下载地址(将批处理中的gc日志地址改为需要监听的日志地址即可使用) :http://loon-simple.googlecode.com/files/GCLog.7z