001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
026     * @author Shuyang Zhou
027     */
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    }