码迷,mamicode.com
首页 > 其他好文 > 详细

ManagementFactory (二) getMemoryMXBean

时间:2016-08-05 19:45:35      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:

MemoryMXBean

 

package cn.zno.outofmomery;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.util.ArrayList;
import java.util.List;

public class Test {
    MemoryMXBean memoryMXBean;
    {
        memoryMXBean = ManagementFactory.getMemoryMXBean();
        System.out.println(memoryMXBean.isVerbose());
    }

    void h(){
        List<byte[]> list = new ArrayList<byte[]>();
        while(true){
//          System.out.println(memoryMXBean.getNonHeapMemoryUsage());
            System.out.println(memoryMXBean.getHeapMemoryUsage());
            list.add(new byte[1024*1024]);
        }
    }
    
    public static void main(String[] args) {
        new Test().h();
    }
}

 

  • 垃圾回收是否启用
  • 获取堆内存使用情况
  • 获取非堆内存使用情况

技术分享

 

 

运行结果:

true
init = 33554432(32768K) used = 608952(594K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 1657544(1618K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 2706136(2642K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 3754728(3666K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 4803320(4690K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 5851912(5714K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 6900504(6738K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 7949096(7762K) committed = 32440320(31680K) max = 32440320(31680K)
[GC 7762K->7548K(31680K), 0.0032242 secs]
init = 33554432(32768K) used = 8777816(8572K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 9921576(9689K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 11148528(10887K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 12197120(11911K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 13245712(12935K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 14294304(13959K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 15342896(14983K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 16391488(16007K) committed = 32440320(31680K) max = 32440320(31680K)
[GC 16007K->15739K(31680K), 0.0032697 secs]
init = 33554432(32768K) used = 17228792(16824K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 18277384(17849K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 19325976(18873K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 20374568(19897K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 21423160(20921K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 22471752(21945K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 23520344(22969K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 24568936(23993K) committed = 32440320(31680K) max = 32440320(31680K)
[Full GC 23993K->23932K(31680K), 0.0051448 secs]
init = 33554432(32768K) used = 25554984(24956K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 26644712(26020K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 27693304(27044K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 28741896(28068K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 29790488(29092K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 30839080(30116K) committed = 32440320(31680K) max = 32440320(31680K)
init = 33554432(32768K) used = 31887672(31140K) committed = 32440320(31680K) max = 32440320(31680K)
[Full GC 31140K->31100K(31680K), 0.0034119 secs]
[Full GC 31100K->31090K(31680K), 0.0033546 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at cn.zno.outofmomery.Test.h(Test.java:19)
    at cn.zno.outofmomery.Test.main(Test.java:23)

垃圾回收格式解读

init = 33554432(32768K) used = 24568936(23993K) committed = 32440320(31680K) max = 32440320(31680K)
[Full GC 23993K->23932K(31680K), 0.0051448 secs]

-----------------------------------------------------------
[Full GC gc前used->gc后used(max), 耗费时间]

 

ManagementFactory (二) getMemoryMXBean

标签:

原文地址:http://www.cnblogs.com/zno2/p/4571771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!