繁体   English   中英

无需重新启动即可生成 Java 线程转储。

[英]Generate a Java thread dump without restarting.

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

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

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

以下是我们如何以编程方式执行此操作: 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;
}

要将线程转储到标准输出,您可以执行以下操作

ThreadInfo[] threads = ManagementFactory.getThreadMXBean()
        .dumpAllThreads(true, true);
for (ThreadInfo info : threads) {
    System.out.print(info);
}

在 Java 6 中使用ThreadMXBean类。 但我建议使用真正的日志记录而不是标准输出。

尝试“kill –QUIT” Process_id 例如

kill -QUIT 2134

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

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

在 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()方法之前添加条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM