如何以编程方式找出我的PermGen空间使用情况?

我正在尝试在Sun的Hotspot JVM上运行时诊断java.lang.OutOfMemoryError: PermGen Space错误,并且想知道我的程序在各个点使用了多少PermGen空间。 有没有办法以编程方式查找此信息?

你可以使用这样的东西:

 Iterator iter = ManagementFactory.getMemoryPoolMXBeans().iterator(); while (iter.hasNext()) { MemoryPoolMXBean item = iter.next(); String name = item.getName(); MemoryType type = item.getType(); MemoryUsage usage = item.getUsage(); MemoryUsage peak = item.getPeakUsage(); MemoryUsage collections = item.getCollectionUsage(); } 

这将为您提供所有类型的内存。 您对“Perm Gen”类型感兴趣。

尝试ManagementFactory.getMemoryPoolMXBeans()