![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package com.tj; 2 3 public class getHeapInfo { 4 public static void main(String[] args) { 5 //获取当前堆的大小 byte 单位 6 long heapSize = Runtime.getRuntime().totalMemory(); 7 System.out.println(heapSize); 8 9 //获取堆的最大大小byte单位 10 //超过将抛出 OutOfMemoryException 11 long heapMaxSize = Runtime.getRuntime().maxMemory(); 12 System.out.println(heapMaxSize); 13 14 //获取当前空闲的内存容量byte单位 15 long heapFreeSize = Runtime.getRuntime().freeMemory(); 16 System.out.println(heapFreeSize); 17 } 18 }
获取堆的大小信息