MemoryPoolMXBean获取堆初始值代码
时间: 2023-03-25 20:01:36 浏览: 42
可以使用以下代码获取堆初始值:
MemoryPoolMXBean heapMemoryPool = ManagementFactory.getMemoryPoolMXBeans()
.stream()
.filter(memoryPool -> "Heap".equals(memoryPool.getType()))
.findFirst()
.orElseThrow(NoSuchElementException::new);
long initHeapSize = heapMemoryPool.getUsage().getInit();
相关问题
MemoryPoolMXBean获取非堆初始值代码
可以使用以下代码获取非堆初始值:
MemoryPoolMXBean nonHeapMemoryPool = ManagementFactory.getMemoryPoolMXBeans().stream()
.filter(memoryPoolMXBean -> !memoryPoolMXBean.isHeapMemory())
.findFirst()
.orElseThrow(() -> new RuntimeException("No non-heap memory pool found"));
long nonHeapInit = nonHeapMemoryPool.getUsage().getInit();
MemoryMXBean获取的内存情况跟MemoryPoolMXBean获取的有什么区别
MemoryMXBean获取的是整个JVM的内存使用情况,包括堆内存和非堆内存的使用情况,而MemoryPoolMXBean获取的是JVM中各个内存池的使用情况,包括Eden区、Survivor区、Old区等。因此,MemoryMXBean提供了更全面的内存使用情况,而MemoryPoolMXBean则提供了更细粒度的内存使用情况。