//  Get memory pools.
static java.util.List<MemoryPoolMXBean> mpools = ManagementFactory
.getMemoryPoolMXBeans(); //  Total number of memory pools.
static int numPools = mpools.size();
/**
 * Returns maximum memory available in the system.
 */
public static float getMaxMem(int npool)
{
MemoryPoolMXBean mp = mpools.get(npool);--------这里报错
float totalMemory = mp.getUsage().getMax();
return totalMemory;
} /**
 * Returns current used memory in the system.
 */
public static float getUsedMemory(int npool)
{
MemoryPoolMXBean mp = mpools.get(npool);--------这里报错
float usedMemory = mp.getUsage().getUsed();
return usedMemory;
};有高人知道是什么问题吗?

解决方案 »

  1.   

    mpools.get(npool);--------这里报错   // 应该没有这个方法的你的程序得到了应该是Java 虚拟机管理的内存,并不是实际的物理内存量
    getMemoryPoolMXBeans() 
              返回 Java 虚拟机中的 MemoryPoolMXBean 对象列表。
    public interface MemoryPoolMXBean内存池的管理接口。内存池表示由 Java 虚拟机管理的内存资源,由一个或多个内存管理器对内存池进行管理。 Java 虚拟机具有此接口的实现类的一个或多个实例。实现此接口的实例是 MXBean,可以通过调用 ManagementFactory.getMemoryPoolMXBeans() 方法或从平台 MBeanServer 方法获得。 在 MBeanServer 中惟一标识内存池的 MXBean 的 ObjectName 为: java.lang:type=MemoryPool,name=pool's name 
      

  2.   

    java.util 
    Interface List<E>
    ======================== E get(int index) 
              Returns the element at the specified position in this list.
      

  3.   

    报错信息Exception in thread "Thread-1" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
      

  4.   

    越界 for (int i = 0; i < numPools; i++)
    {
    totalUsedMemory = totalUsedMemory + getUsedMemory(i);
    }
    我以前写
    for (int i = 1; i <= numPools; i++)
    {
    totalUsedMemory = totalUsedMemory + getUsedMemory(i);
    }