简体   繁体   English

Tomcat获取Java堆空间

[英]Tomcat get Java Heap Space

Is there a way to find out the Java Heap Space of my Tomcat Server (7.0.67) by using Java code ? 有没有一种方法可以使用Java代码找出我的Tomcat服务器(7.0.67)的Java堆空间?

I tired to get it with CatalinaProperties.getProperty() but I have no clue how the Property for the Heap Space is called exactly. 我讨厌用CatalinaProperties.getProperty()来获取它,但是我不知道如何正确调用堆空间的属性。 The next thing I checked was the catalina.properties file but there is no entry for the Heap Space... 我检查的下一件事是catalina.properties文件,但是没有堆空间条目...

This one gets me all I want: 这让我得到了我想要的一切:

    /**
     * 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();
    }

I just see that I used some non-Java format functions, but the answer should be useful anyway. 我只是看到我使用了一些非Java格式的函数,但是无论如何答案都应该是有用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM