import java.lang.management.*;

class Memory {
    public static void main(String[] args) {
        MemoryMXBean m = ManagementFactory.getMemoryMXBean();
        for(MemoryType type: MemoryType.values()) {
            usage(type, type == MemoryType.HEAP?
                m.getHeapMemoryUsage(): m.getNonHeapMemoryUsage());
            System.out.println();
            for(MemoryPoolMXBean mp: ManagementFactory.getMemoryPoolMXBeans())
                if(mp.getType() == type) usage(mp.getName(), mp.getUsage());
            System.out.println();
        }
    }
  
    private static void usage(Object header, MemoryUsage mu) {
        long used = mu.getUsed(), max = mu.getMax();
        System.out.printf(
            max > 0? "%-30s %,d (%,d MiB) of %,d (%,d MiB)%n": "%-30s %,d (%,d MiB)%n",
            header, used, used >>> 20, max, max >>> 20);
    }
}
