生成Java线程转储而不重新启动。

时间:2012-10-11 14:48:30

标签: java multithreading heap dump

我想创建一个跟踪内存使用情况和CPU使用情况的线程。

如果应用程序达到高级别,我想生成堆转储或线程转储。

有没有办法在不重新启动的情况下生成线程转储运行时?

5 个答案:

答案 0 :(得分:11)

以下是我们如何以编程方式执行此操作:http://pastebin.com/uS5jYpd4

我们使用JMX ThreadMXBeanThreadInfo类:

ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = mxBean.getThreadInfo(mxBean.getAllThreadIds(), 0);
...

您也可以在~unix下执行kill -QUIT pid将堆栈转储到标准输出。还有jstack来转储JVM的堆栈。

如果应用程序的负载平均值高于某个阈值,我们还有一个自动转储堆栈:

private long lastCpuTimeMillis;
private long lastPollTimeMillis;

public void checkLoadAverage() {
    long now = System.currentTimeMillis();
    long currentCpuMillis = getTotalCpuTimeMillis();
    double loadAvg = calcLoadAveragePercentage(now, currentCpuMillis);
    if (loadAvg > LOAD_AVERAGE_DUMP_THRESHOLD) {
        try {
            dumpStack("Load average percentage is " + loadAvg);
        } catch (IOException e) {
            // Oh well, we tried
        }
    }
    lastCpuTimeMillis = currentCpuMillis;
    lastPollTimeMillis = now;
}

private long getTotalCpuTimeMillis() {
    long total = 0;
    for (long id : threadMxBean.getAllThreadIds()) {
        long cpuTime = threadMxBean.getThreadCpuTime(id);
        if (cpuTime > 0) {
            total += cpuTime;
        }
    }
    // since is in nano-seconds
    long currentCpuMillis = total / 1000000;
    return currentCpuMillis;
}

private double calcLoadAveragePercentage(long now, long currentCpuMillis) {
    long timeDiff = now - lastPollTimeMillis;
    if (timeDiff == 0) {
        timeDiff = 1;
    }
    long cpuDiff = currentCpuMillis - lastCpuTimeMillis;
    double loadAvg = (double) cpuDiff / (double) timeDiff;
    return loadAvg;
}

答案 1 :(得分:9)

要将线程转储到标准输出,您可以执行类似这样的操作

ThreadInfo[] threads = ManagementFactory.getThreadMXBean()
        .dumpAllThreads(true, true);
for(final ThreadInfo info : threads)
    System.out.print(info);
在Java 6中使用ThreadMXBean

。但我建议使用真实的日志而不是标准输出。

答案 2 :(得分:2)

尝试 “kill -QUIT”Process_id 例如

kill -QUIT 2134

这将触发线程转储而不重新启动它

答案 3 :(得分:0)

是的,您可以使用内置管理MXBeans生成自己的堆栈转储。具体来说,您可以从ThreadInfos获取所有当前ThreadMXBean并将内容写入您想要的位置。

答案 4 :(得分:0)

在spring应用程序中,我们可以通过创建cron作业以编程方式生成堆转储。

import com.sun.management.HotSpotDiagnosticMXBean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.management.MBeanServer;
import java.io.File;
import java.lang.management.ManagementFactory;

@Component
public class HeapDumpGenerator {
    @Scheduled(cron = "0 0/5 * * * *")
    public void execute() {
        generateHeapDump();
    }

    private void generateHeapDump() {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        String dumpFilePath = System.getProperty("user.home") + File.separator + 
                "heap-dumps" + File.separator +
                "dumpfile_" + System.currentTimeMillis() + ".hprof";
        try {
            HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean = ManagementFactory
                .newPlatformMXBeanProxy(mBeanServer,
                                    "com.sun.management:type=HotSpotDiagnostic", 
                                    HotSpotDiagnosticMXBean.class);
            hotSpotDiagnosticMXBean.dumpHeap(dumpFilePath, Boolean.TRUE);
        } catch (Exception e) {
            // log error
        }
    }
}

它每五分钟运行一次,但是我们可以设置自己的时间/间隔。我们还可以在调用generateHeapDump()方法之前添加条件。

相关问题