是否可以用与查找堆大小相同的方式显示java NewSize参数?

我知道可以使用以下方法找到堆大小:

Runtime.getRuntime().maxMemory();


NewSize有类似的东西吗?

最佳答案

您希望MemoryPoolMXBean获得伊甸空间和幸存者空间的数值:

for(java.lang.management.MemoryPoolMXBean memoryPoolMXBean :java.lang.management.ManagementFactory.getMemoryPoolMXBeans())
{
    System.out.println("");
    try
    {
        System.out.println("PoolName\t" + memoryPoolMXBean.getName());
        System.out.println("Commited\t" + memoryPoolMXBean.getCollectionUsage().getCommitted());
        System.out.println("Init\t" + memoryPoolMXBean.getCollectionUsage().getInit());
        System.out.println("Max\t" + memoryPoolMXBean.getCollectionUsage().getMax());
        System.out.println("Used\t" + memoryPoolMXBean.getCollectionUsage().getUsed());
    }
    catch (NullPointerException npex)
    {
        npex.printStackTrace();
    }
}


输出:

PoolName    Code Cache
java.lang.NullPointerException
    at com.github.lyubent.example.App.main(App.java:40)

PoolName    Par Eden Space
Commited    0
Init    335544320
Max 335544320
Used    0

PoolName    Par Survivor Space
Commited    0
Init    41943040
Max 41943040
Used    0

PoolName    CMS Old Gen
Commited    0
Init    1728053248
Max 1728053248
Used    0

PoolName    CMS Perm Gen
Commited    0
Init    21757952
Max 85983232
Used    0

09-11 19:22