001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.process.ProcessUtil;
018
019 import java.lang.management.ManagementFactory;
020 import java.lang.management.RuntimeMXBean;
021
022 import java.util.ArrayList;
023 import java.util.List;
024
025
028 public class HeapUtil {
029
030 public static void heapDump(boolean live, boolean binary, String file) {
031 int processId = _getProcessId();
032
033 StringBundler sb = new StringBundler(5);
034
035 sb.append("-dump:");
036
037 if (live) {
038 sb.append("live,");
039 }
040
041 if (binary) {
042 sb.append("format=b,");
043 }
044
045 sb.append("file=");
046 sb.append(file);
047
048 List<String> arguments = new ArrayList<String>();
049
050 arguments.add("jmap");
051 arguments.add(sb.toString());
052 arguments.add(String.valueOf(processId));
053
054 try {
055 ProcessUtil.execute(
056 ProcessUtil.LOGGING_OUTPUT_PROCESSOR, arguments);
057 }
058 catch (Exception e) {
059 throw new RuntimeException("Unable to perform heap dump", e);
060 }
061 }
062
063 private static int _getProcessId() {
064 RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
065
066 String name = runtimeMXBean.getName();
067
068 int index = name.indexOf(CharPool.AT);
069
070 if (index == -1) {
071 throw new RuntimeException("Unable to parse process name " + name);
072 }
073
074 int pid = GetterUtil.getInteger(name.substring(0, index));
075
076 if (pid == 0) {
077 throw new RuntimeException("Unable to parse process name " + name);
078 }
079
080 return pid;
081 }
082
083 }