项目出现内存溢出现象,我想大牛们都会遇到过。最近做项目又遇到了,后来加一下内存监控逻辑,很简单一个代码,我看zuidaima没有关于这方面的,因此分享给有可能需要的牛牛们,也欢迎大牛提供更好的思路。
01 |
public static void main(String[]
args) throws Exception{ |
02 |
Runtime
run = Runtime.getRuntime(); |
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; |
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); |
执行结果:(我的VM参数配置:-Xms32m -Xmx800m)
最大内存 = 832438272
已分配内存 = 33357824
已分配内存中的已使用空间 = 314816
已分配内存中的剩余空间 = 33043008
最大可用内存 = 832123456
文章出处:http://www.zuidaima.com/share/1581528986160128.htm