JVM内存检测

来源:互联网 发布:淘宝商城户外用品 编辑:程序博客网 时间:2024/06/05 04:48

项目出现内存溢出现象,我想大牛们都会遇到过。最近做项目又遇到了,后来加一下内存监控逻辑,很简单一个代码,我看zuidaima没有关于这方面的,因此分享给有可能需要的牛牛们,也欢迎大牛提供更好的思路。

01public static void main(String[] args) throws Exception{
02      Runtime run = Runtime.getRuntime();
03 
04      long max = run.maxMemory();
05      long total = run.totalMemory();
06      long free = run.freeMemory();
07      long usedable = total - free;
08      long usable = max - total + free;
09 
10      System.out.println("最大内存 = " + max);
11      System.out.println("已分配内存 = " + total);
12      System.out.println("已分配内存中的已使用空间 = " + usedable);
13      System.out.println("已分配内存中的剩余空间 = " + free);
14      System.out.println("最大可用内存 = " + usable);
15      System.out.println(); 
16       
17  }//main -- end

执行结果:(我的VM参数配置:-Xms32m -Xmx800m)

最大内存 = 832438272
已分配内存 = 33357824
已分配内存中的已使用空间 = 314816
已分配内存中的剩余空间 = 33043008
最大可用内存 = 832123456

文章出处:http://www.zuidaima.com/share/1581528986160128.htm

0 0