请教getCommittedVirtualMemorySize() 到底返回的是什么?

UOWwg934 2015-02-05 11:30:01
在一段代码里看到如下逻辑: 如果getCommittedVirtualMemorySize()的值大于6倍maxMemory()那么就会关掉这个java进程

if (mxbean.getCommittedVirtualMemorySize()> run.maxMemory()* 6) {
..........
Main.getInstance().shutdown();
}

请教一下 maxMemory()和getCommittedVirtualMemorySize() 两个函数的区别:
尤其是getCommittedVirtualMemorySize() 究竟取的什么值



Oracle API 对于两个函数的描写如下

1. maxMemory():
Returns the maximum amount of memory that the Java virtual machine will attempt to use.
返回JVM可用的最大内存, 我的理解是-Xmx 设置的最大堆内存大小

2. getCommittedVirtualMemorySize() :
Returns the amount of virtual memory that is guaranteed to be available to the running process in bytes, or -1 if this operation is not supported.
返回java运行进程保证可用的虚拟内存大小 ---> 这里我就不是很理解到底是什么意思了。

我的测试代码如下,依然没能看明白getCommittedVirtualMemorySize()返回的什么值:

public class memtest{
public static void main(String[] args){
Runtime run = Runtime.getRuntime();
long total;
OperatingSystemMXBean osm = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

System.out.println("##################Before Testing#####################");
System.out.println("Max Memory= "+(run.maxMemory())/1024.0/1024);
System.out.println("Allocated Memory (used) = "+ run.totalMemory()/1024.0/1024);
System.out.println("Free on Allocated Memory = "+ run.freeMemory()/1024.0/1024);
System.out.println("Left Memory = "+ (run.maxMemory()-run.totalMemory()+run.freeMemory())/1024.0/1024);
System.out.println("getCommittedVirtualMemorySize() "+osm.getCommittedVirtualMemorySize()/1024.0/1024);

System.out.println("#******************** Testing ***********************");
ArrayList ii = new ArrayList();
int j=0;
for(int i=0;i<1073741800;i++)
{
ii.add('a');
if (i%(1024)==0){
j=j+1;
System.out.println("#--------Round: "+j+" Allocated Memory: "+i);
System.out.println("Max Memory= "+(run.maxMemory())/1024.0/1024);
System.out.println("Allocated Memory (used) = "+ run.totalMemory()/1024.0/1024);
System.out.println("Free on Allocation Memory = "+ run.freeMemory()/1024.0/1024);
System.out.println("Left Memory = "+ (run.maxMemory()-run.totalMemory()+run.freeMemory())/1024.0/1024);
System.out.println("getCommittedVirtualMemorySize() "+osm.getCommittedVirtualMemorySize()/1024.0/1024);
}


linux上执行结果:我让他一直分配内存 直到OOO
[root@BJHS23A-changme ~]# java -Xmx512m memtest > memtest.txt
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2772)
at java.util.Arrays.copyOf(Arrays.java:2746)
at java.util.ArrayList.ensureCapacity(ArrayList.java:187)
at java.util.ArrayList.add(ArrayList.java:378)
at memtest.main(memtest.java:29)

[root@BJHS23A-changme ~]# vim memtest.txt

##################Before Testing#####################
Max Memory= 455.125
Allocated Memory (used) = 8.6875
Free on Allocated Memory = 8.392105102539062
Left Memory = 454.82960510253906
getCommittedVirtualMemorySize() 753.84765625
#******************** Testing ***********************
#--------Round: 1 Allocated Memory: 0
Max Memory= 455.125
Allocated Memory (used) = 8.6875
Free on Allocation Memory = 8.392105102539062
Left Memory = 454.82960510253906
getCommittedVirtualMemorySize() 753.84765625
。。。。。。。。。
#--------Round: 25162 Allocated Memory: 25764864
Max Memory= 455.125
Allocated Memory (used) = 372.4375
Free on Allocation Memory = 15.164581298828125
Left Memory = 97.85208129882812
getCommittedVirtualMemorySize() 862.02734375

第25162次分配后 OOO。
...全文
331 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
UOWwg934 2015-02-05
  • 打赏
  • 举报
回复
在线等 求解答。。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧