JVM GC日志经常要检查,可以提前发现问题。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
/**
create table test
(
file_name varchar2(100),
id number,
ParOldGen number,
ParOldGen_begin number,
ParOldGen_end number,
PermGen number,
PermGen_begin number,
PermGen_end number,
gc_time number
);
select file_name,ParOldGen_begin,ParOldGen_end from
(select file_name,
row_number() over(partition by file_name order by id desc) rn,
ParOldGen_begin,
ParOldGen_end
from test) where rn=1
*/
public class ReadGCLog {
static final String driver_class = “oracle.jdbc.driver.OracleDriver”;
static final String connectionURL = “jdbc:oracle:thin:@10.10.17.16:1521:orcl”;
static final String userID = “test”;
static final String userPassword = “test”;
public static void main(String argv[]){
String path = "E:\\gc_logs";
File file = new File(path);
File[] fs = file.listFiles();
for(File filePath:fs){
if(!filePath.isDirectory()){
System.out.println("开始读取:"+filePath);
readTxtFile(filePath.toString());
System.out.println("结束读取:"+filePath);
}
}
}
public static void readTxtFile(String filePath){
Connection con = null;
String s_sql = "insert into test values(?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = null;
int i=0;
try {
Class.forName (driver_class).newInstance();
con = DriverManager.getConnection(connectionURL, userID, userPassword);
pstmt = con.prepareStatement(s_sql);
con.setAutoCommit(false);
File file=new File(filePath);
InputStreamReader read = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt =null;
String[] strFileName = filePath.split("\\\\");
String fileName = strFileName[strFileName.length-1];
while((lineTxt = bufferedReader.readLine()) != null){
if(lineTxt.contains("Full GC")){
String[] str = lineTxt.split(" ");
String[] strParOldGen = str[7].replace(")]", "").replace("(", "->").split("->");
String[] strPermGen = str[10].replace(")],", "").replace("(", "->").split("->");
pstmt.setString(1, fileName);
pstmt.setInt(2, i);
pstmt.setInt(3, Integer.valueOf(strParOldGen[2].replace("K", "")));
pstmt.setInt(4, Integer.valueOf(strParOldGen[0].replace("K", "")));
pstmt.setInt(5, Integer.valueOf(strParOldGen[1].replace("K", "")));
pstmt.setInt(6, Integer.valueOf(strPermGen[2].replace("K", "")));
pstmt.setInt(7, Integer.valueOf(strPermGen[0].replace("K", "")));
pstmt.setInt(8, Integer.valueOf(strPermGen[1].replace("K", "")));
pstmt.setFloat(9, Float.valueOf(str[11]));
pstmt.addBatch();
i++;
}
}
pstmt.executeBatch();
con.commit();
read.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pstmt != null){
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
pstmt = null;
}
}
if(con != null){
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
con = null;
}
}
}
}
}