[英]Generate a Java thread dump without restarting.
I'd like to create a thread that keeps track of the memory usage and cpu usage.我想创建一个线程来跟踪内存使用情况和 CPU 使用情况。
If the application reaches a high level, I want to generate an heap dump or a thread dump.如果应用程序达到高水平,我想生成堆转储或线程转储。
Is there a way to generate a Thread dump runtime without restarting?有没有办法在不重新启动的情况下生成线程转储运行时?
Here's how we do it programmatically: http://pastebin.com/uS5jYpd4以下是我们如何以编程方式执行此操作: http : //pastebin.com/uS5jYpd4
We use the JMX
ThreadMXBean
and ThreadInfo
classes:我们使用
JMX
ThreadMXBean
和ThreadInfo
类:
ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = mxBean.getThreadInfo(mxBean.getAllThreadIds(), 0);
...
You can also do a kill -QUIT pid
under ~unix to dump the stacks to the standard-out.您还可以在 ~unix 下执行
kill -QUIT pid
将堆栈转储到标准输出。 There is also jstack to dump the stack of a JVM.还有 jstack 用于转储 JVM 的堆栈。
We also have an automation which dumps the stack if the load average of the application is above some threshold:如果应用程序的平均负载高于某个阈值,我们还有一个自动转储堆栈:
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;
}
To dump the threads to the standard out, you may do something like this要将线程转储到标准输出,您可以执行以下操作
ThreadInfo[] threads = ManagementFactory.getThreadMXBean()
.dumpAllThreads(true, true);
for (ThreadInfo info : threads) {
System.out.print(info);
}
in Java 6 using the ThreadMXBean class.在 Java 6 中使用ThreadMXBean类。 But I would suggest to use real logging instead of the standard output.
但我建议使用真正的日志记录而不是标准输出。
Try “kill –QUIT” Process_id eg尝试“kill –QUIT” Process_id 例如
kill -QUIT 2134
This will trigger the thread dump without restarting it这将触发线程转储而不重新启动它
Yes, you can generated your own stack dump using the built-in management MXBeans.是的,您可以使用内置的管理 MXBean 生成自己的堆栈转储。 Specifically, you can get all the current ThreadInfos From the ThreadMXBean and write the contents to your desired location.
具体来说,您可以从ThreadMXBean获取所有当前ThreadInfos并将内容写入您想要的位置。
In spring application, we can generate heap dump programmatically by creating cron job.在 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
}
}
}
It will run at every five minutes, but we can set our own time/interval.它将每五分钟运行一次,但我们可以设置自己的时间/间隔。 We can also add condition before calling
generateHeapDump()
method.我们也可以在调用
generateHeapDump()
方法之前添加条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.