再会!我无法获取 java 线程的内存使用情况。我的研究把我带到了 ThreadMxBean 库。根据javadocThreadMXBean#setThreadAllocatedMemoryEnabled
,应该有一个方法getThreadAllocatedBytes
允许获取线程的内存。但是,我找不到这种类方法,而存在上述文档中描述的其他方法。
示例getCurrentThreadCpuTime()
并isThreadCpuTimeEnabled()
显示在我的代码中。
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MalformedObjectNameException;
import javax.management.ReflectionException;
class TwoThreadsTest {
public static void main (String[] args) throws Exception {
new Coding("Jamaica").start();
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long b = threadBean.getCurrentThreadCpuTime();
boolean bool= threadBean.isThreadCpuTimeEnabled();
System.out.println(bool);
}
}