gpt4 book ai didi

java - Tomcat 获取 Java 堆空间

转载 作者:行者123 更新时间:2023-11-28 22:41:09 25 4
gpt4 key购买 nike

有没有办法通过 Java 代码找出我的 Tomcat 服务器 (7.0.67) 的 Java 堆空间?

我厌倦了使用 CatalinaProperties.getProperty() 获取它,但我不知道如何准确调用堆空间的属性。接下来我检查的是 catalina.properties 文件,但没有堆空间条目...

最佳答案

这个让我得到了我想要的一切:

    /**
* Returns a large bunch of memory information.
*/
public static String getAllMemoryInformation() {
StringBuilder b = new StringBuilder();
b.append("Current PID: "+getPid()+"\n"); //$NON-NLS-1$

com.sun.management.OperatingSystemMXBean osb =
(com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
b.append("Physical memory: "+formatMemory(osb.getTotalPhysicalMemorySize())+" total, "+ //$NON-NLS-1$ //$NON-NLS-2$
formatMemory(osb.getFreePhysicalMemorySize())+" free.\n"); //$NON-NLS-1$
b.append("Swap space: "+formatMemory(osb.getTotalSwapSpaceSize())+" total, "+ //$NON-NLS-1$ //$NON-NLS-2$
formatMemory(osb.getFreeSwapSpaceSize())+" free.\n"); //$NON-NLS-1$

MemoryUsage m = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
b.append("Heap memory usage: "+formatMemory(m.getInit())+" initial, "+ //$NON-NLS-1$ //$NON-NLS-2$
formatMemory(m.getUsed())+" used, "+ //$NON-NLS-1$
formatMemory(m.getCommitted())+" committed, "+ //$NON-NLS-1$
formatMemory(m.getMax())+" max.\n"); //$NON-NLS-1$
for( MemoryPoolMXBean mpb : ManagementFactory.getMemoryPoolMXBeans() ) {
if( mpb.getType().equals(MemoryType.HEAP) ) {
m = mpb.getUsage();
b.append("- " + mpb.getName()+" usage: "+formatMemory(m.getInit())+" initial, "+ //$NON-NLS-2$ //$NON-NLS-3$
formatMemory(m.getUsed())+" used, "+ //$NON-NLS-1$
formatMemory(m.getCommitted())+" committed, "+ //$NON-NLS-1$
formatMemory(m.getMax())+" max.\n"); //$NON-NLS-1$
}
}

m = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
b.append("Non-Heap memory usage: "+formatMemory(m.getInit())+" initial, "+ //$NON-NLS-1$ //$NON-NLS-2$
formatMemory(m.getUsed())+" used, "+ //$NON-NLS-1$
formatMemory(m.getCommitted())+" committed, "+ //$NON-NLS-1$
formatMemory(m.getMax())+" max.\n"); //$NON-NLS-1$
for( MemoryPoolMXBean mpb : ManagementFactory.getMemoryPoolMXBeans() ) {
if( mpb.getType().equals(MemoryType.NON_HEAP) ) {
m = mpb.getUsage();
b.append("- " + mpb.getName() + " usage: "+formatMemory(m.getInit())+" initial, "+ //$NON-NLS-2$ //$NON-NLS-3$
formatMemory(m.getUsed())+" used, "+ //$NON-NLS-1$
formatMemory(m.getCommitted())+" committed, "+ //$NON-NLS-1$
formatMemory(m.getMax())+" max.\n"); //$NON-NLS-1$
}
}
return b.toString();
}

我只是看到我使用了一些非Java格式的函数,但无论如何答案应该有用。

关于java - Tomcat 获取 Java 堆空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35131585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com