diff --git a/symmetric-server/src/main/java/org/jumpmind/symmetric/SymmetricLauncher.java b/symmetric-server/src/main/java/org/jumpmind/symmetric/SymmetricLauncher.java index fd4ea35fa1..c54de05852 100644 --- a/symmetric-server/src/main/java/org/jumpmind/symmetric/SymmetricLauncher.java +++ b/symmetric-server/src/main/java/org/jumpmind/symmetric/SymmetricLauncher.java @@ -22,10 +22,19 @@ import static org.apache.commons.lang.StringUtils.isNotBlank; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.Options; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.io.File; +import java.lang.management.ManagementFactory; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.io.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.slf4j.MDC; /** @@ -125,7 +134,8 @@ protected boolean executeWithOptions(CommandLine line) throws Exception { String httpBasicAuthUser = null; String httpBasicAuthPassword = null; - configureCrypto(line); + configureCrypto(line); + removeOldHeapDumps(); if (line.hasOption(OPTION_HTTP_BASIC_AUTH_USER) && line.hasOption(OPTION_HTTP_BASIC_AUTH_PASSWORD)) { @@ -191,10 +201,9 @@ public void run() { } }; } - + if (line.hasOption(OPTION_START_CLIENT)) { getSymmetricEngine(false).start(); - return true; } else { SymmetricWebServer webServer = new SymmetricWebServer(chooseWebDir(line, webDir), maxIdleTime, propertiesFile != null ? propertiesFile.getCanonicalPath() : null, @@ -238,11 +247,52 @@ public void run() { } webServer.start(); - - return true; - } - + } + return true; } + + protected void removeOldHeapDumps() { + String heapDumpPath = null; + List options = ManagementFactory.getRuntimeMXBean().getInputArguments(); + for (String option : options) { + if (option.startsWith("-XX:HeapDumpPath=")) { + heapDumpPath = option.split("=")[1]; + } + } + if (log.isDebugEnabled()) { + log.debug("Heap dump path is " + (heapDumpPath == null ? null : new File(heapDumpPath).getAbsolutePath())); + } + + if (heapDumpPath != null) { + File directory = new File(heapDumpPath); + Collection files = FileUtils.listFiles(directory, new String[] { "hprof" }, false); + List recentFiles = new ArrayList(); + List oldFiles = new ArrayList(); + + for (File file : files) { + if (file.lastModified() < (System.currentTimeMillis() - 604800000)) { + oldFiles.add(file); + } else { + recentFiles.add(file); + } + } + + Collections.sort(recentFiles, new Comparator() { + public int compare(File f1, File f2) { + return f1.lastModified() > f2.lastModified() ? -1 : 1; + } + }); + + for (int i = 2; i < recentFiles.size(); i++) { + oldFiles.add(recentFiles.get(i)); + } + + for (File file : oldFiles) { + log.info("Removing old heap dump " + file.getName()); + file.delete(); + } + } + } protected String chooseWebDir(CommandLine line, String webDir) { return webDir;