Tomcat得到Java堆Space

Tomcat get Java Heap Space

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

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

的条目

这个能满足我的所有需求:

    /**
     * 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格式的函数,但无论如何答案应该有用。