公司服务器上Tomcat不知哪个项目有隐患代码,经常溢出,不停的重启! 起初打算用Jprofiler监控下内存(回头会准备个完整的例子),但发现线上环境bin目录下的.sh被修改的乱七八糟,导致某些.sh无法运行,所以就打算写个RMI的定时gc程序,现用虚拟机模拟下线上环境
环境信息如下:
Red Hat Enterprise Linux Server release 6.3 (Santiago)
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
apache-tomcat-6.0.41
jprofiler7 linux-x64
ip: 10.211.55.4
public static void main(String[] args) throws IOException, MalformedObjectNameException {
Properties properties = Console.parseArguments(args);
//
final String host = properties.getProperty(HOST, "localhost");
final String port = properties.getProperty(PORT, "1099");
final String interval = properties.getProperty(INTERVAL);
final String tryCount = properties.getProperty(TRY);
final String rmiServer = getRMIServer(host, port);
final String user = properties.getProperty(USER);
final String pass = properties.getProperty(PASS);
final String report = properties.getProperty(REPORT, "./gc.out");
//
final Invoke invoke = new Invoke() {
@Override
public Object execute(JMXConnector connector) throws IOException, MalformedObjectNameException {
MBeanServerConnection mBeanServerConnection = connector.getMBeanServerConnection();
MemoryMXBean memoryMXBean = JMX.newMBeanProxy(mBeanServerConnection, ObjectName.getInstance("java.lang:type=Memory"), MemoryMXBean.class);
memoryMXBean.gc();
return null;
}
};
if (interval == null) {
execute(rmiServer, user, pass, invoke);
} else {
final int i = interval.matches("\\d+") ? Integer.parseInt(interval) : 6000;
final int t = tryCount.matches("\\d+") ? Integer.parseInt(tryCount) : 3;
final File reportFile = new File(report);
if (!reportFile.exists()) {
reportFile.createNewFile();
} else if (reportFile.isDirectory()) {
throw new RuntimeException(new MessageFormat("can`t write file {0},file is directory!").format(new String[]{reportFile.getAbsolutePath()}));
} else if (!reportFile.canWrite()) {
throw new RuntimeException(new MessageFormat("can`t write file {0},file is readonly!").format(new String[]{reportFile.getAbsolutePath()}));
}
new Thread(new Runnable() {
private final ThreadLocal<AtomicInteger> error = new ThreadLocal<AtomicInteger>();
private final ThreadLocal<AtomicInteger> local = new ThreadLocal<AtomicInteger>();
private final ThreadLocal<Date> start = new ThreadLocal<Date>();