📄 gcanalyzer.java
字号:
package net.betterjava.util;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
public class GCAnalyzer extends Object {
static BufferedReader br = null;
static int minorGC = 0;
static int majorGC = 0;
static long minorMemory = 0;
static long majorMemory = 0;
static double minorTime = 0;
static double majorTime = 0;
private class GCstatistic {
public long memory = 0;
public double duration = 0;
public GCstatistic(long m, double d) {
memory = m;
duration = d;
}
}
private static double parse2Spaces(String remain) {
int sec = remain.indexOf("secs]");
if (sec == -1)
return 0;
int leftSpaceIndex = remain.indexOf(" ");
String temp = remain.substring(leftSpaceIndex + 1);
int rightSpaceIndex = temp.indexOf(" ");
temp = temp.substring(0, rightSpaceIndex);
return Double.valueOf(temp).doubleValue();
}
private long parseArrow(String remain) {
int indexDash = remain.indexOf("->");
if (indexDash == -1)
return 0;
String temp = remain.substring(0, indexDash - 1);
long leftOfArrow = Long.valueOf(temp).longValue();
int indexBracket = remain.indexOf("(");
if (indexBracket == -1)
return 0;
temp = remain.substring(indexDash + 2, indexBracket - 1);
long rightOfArraow = Long.valueOf(temp).longValue();
return leftOfArrow - rightOfArraow;
}
private GCstatistic parseGC(BufferedReader br, String temp) throws IOException {
int indexOfSecondSquareBracket;
indexOfSecondSquareBracket = temp.lastIndexOf("]");
while (indexOfSecondSquareBracket == -1 && temp != null) {
temp = br.readLine();
if (temp == null) {
return this.new GCstatistic(0, 0);
} else {
indexOfSecondSquareBracket = temp.lastIndexOf("]");
}
}
String noTailSquareBracket = temp.substring(0, indexOfSecondSquareBracket);
indexOfSecondSquareBracket = noTailSquareBracket.lastIndexOf("]");
temp = temp.substring(indexOfSecondSquareBracket + 1).trim();
return this.new GCstatistic(parseArrow(temp), parse2Spaces(temp));
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println(
"usage: java net.betterjava.util.GCAnalyzer ."
+ File.separator
+ "1.1.log");
return;
}
try {
br = new BufferedReader(new FileReader(args[0]));
String s = br.readLine();
GCAnalyzer gca = new GCAnalyzer();
while (s != null) {
s = s.trim();
if (s.indexOf("[GC") != -1) {
minorGC++;
GCstatistic gc = gca.parseGC(br, s);
minorMemory += gc.memory;
minorTime += gc.duration;
}
if (s.indexOf("[Full GC") != -1) {
majorGC++;
GCstatistic gc = gca.parseGC(br, s);
majorMemory += gc.memory;
majorTime += gc.duration;
}
s = br.readLine();
}
System.out.println("minor gc\t" + minorGC);
System.out.println("major gc\t" + majorGC);
System.out.println("minor gc memory\t" + minorMemory + "K");
System.out.println("major gc memory\t" + majorMemory + "K");
System.out.println("minor gc time\t" + minorTime + "secs");
System.out.println("major gc time\t" + majorTime + "secs");
} catch (FileNotFoundException e) {
e.printStackTrace(System.out);
} catch (IOException e) {
e.printStackTrace(System.out);
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -