有没有办法通过使用Java代码找出我的Tomcat服务器(7.0.67)的Java堆空间?
我厌倦了用CatalinaProperties.getProperty()
来获取它但我不知道如何准确调用堆空间的属性。我检查的下一件事是catalina.properties文件,但堆空间没有条目......
答案 0 :(得分:2)
这个让我得到了我想要的一切:
/**
* 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格式的函数,但无论如何答案应该是有用的。