如下是Java 内存消耗程序的源代码。其目的是消耗所有JVM内存(RAM)。它尝试分配1 MB的字节数组,直到用完JVM RAM。
import java.util.Vector;
public class MemoryEater {
public static void main(String[] args) {
Vector v = new Vector();
int i = 0;
while (true) {
byte b[] = new byte[1048576];
v.add(b);
i = i + 1;
Runtime rt = Runtime.getRuntime();
System.out.println("Total Memory = " +
rt.totalMemory() +
" Free Memory = " +
rt.freeMemory() +
" Allocated Memory = " +
i +
"MB");
}
}
}
使用选项(如下所示)运行此Java 内存消耗程序也很重要,-Xmx以确保您的JVM获得了您希望它拥有的所有内存:
$ java -Xmx1024M MemoryEater
上面显示的示例将尝试消耗多达1,024 MB RAM(1 GB)。
IBM J9 JDK 1.4.2 32bit On AIX6.1 64 bit测试结果:
# /usr/java14/bin/java -Xmx6910m MemoryEater
Total Memory = 2950756864 Free Memory = 2948381040
Total Memory = 2950756864 Free Memory = 2947332448
...
...
...
Total Memory = 2950756864 Free Memory = 1073272
JVMDG217: Dump Handler is Processing OutOfMemory - Please Wait.
JVMDG315: JVM Requesting Heap dump file
JVMDG318: Heap dump file written to //heapdump18743486.1608351347.phd
JVMDG303: JVM Requesting Java core file
JVMDG304: Java core file written to //javacore18743486.1608351348.txt
JVMDG274: Dump Handler has Processed OutOfMemory.
JVMST109: Insufficient space in Javaheap to satisfy allocation request
Exception in thread "main" java.lang.OutOfMemoryError
at MemoryEater.main(MemoryEater.java(Compiled Code))
2950756864/1024/1024=2814.061035MB
可见-Xmx6910m并不代表真正可以使用6910m内存。实际最大可以使用2814m,最好实际配置时 -Xmx小于2814m。
另一时间在上面统一台AIX上测试
Total Memory = 2940271104 Free Memory = 2122008
Total Memory = 2940271104 Free Memory = 1073416
JVMDG217: Dump Handler is Processing OutOfMemory - Please Wait.
JVMDG315: JVM Requesting Heap dump file
JVMDG318: Heap dump file written to //heapdump19595290.1608435853.phd
JVMDG303: JVM Requesting Java core file
JVMDG304: Java core file written to //javacore19595290.1608435854.txt
JVMDG274: Dump Handler has Processed OutOfMemory.
JVMST109: Insufficient space in Javaheap to satisfy allocation request
Exception in thread "main" java.lang.OutOfMemoryError
at MemoryEater.main(MemoryEater.java(Compiled Code))
# /usr/java14/bin/java -version
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2)
Classic VM (build 1.4.2, J2RE 1.4.2 IBM AIX build ca142ifx-20130618 (SR13 FP18) (JIT enabled: jitc))
# oslevel -s
6100-09-04-1441
#
另一台AIXJAVA最大可使用内存小很多
Total Memory = 1996618240 Free Memory = 1070744
JVMDG217: Dump Handler is Processing OutOfMemory - Please Wait.
JVMDG315: JVM Requesting Heap dump file
JVMDG318: Heap dump file written to //heapdump119008.1608435566.phd
JVMDG303: JVM Requesting Java core file
JVMDG304: Java core file written to //javacore119008.1608435567.txt
JVMDG274: Dump Handler has Processed OutOfMemory.
JVMST109: Insufficient space in Javaheap to satisfy allocation request
Exception in thread "main" java.lang.OutOfMemoryError
at MemoryEater.main(MemoryEater.java(Compiled Code))
# /usr/java14/bin/java -version
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2)
Classic VM (build 1.4.2, J2RE 1.4.2 IBM AIX build ca142ifx-20110418 (SR13 FP9) (JIT enabled: jitc))
# oslevel -s
5300-12-08-1316
#
将jdk 升级补丁后,JAVA最大可使用内存增大了,应该是SR13 FP9的 一个BUG。
# /usr/java14/bin/java -version
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2)
Classic VM (build 1.4.2, J2RE 1.4.2 IBM AIX build ca142ifx-20130618 (SR13 FP18) (JIT enabled: jitc))
Total Memory = 2940271104 Free Memory = 2123176
Total Memory = 2940271104 Free Memory = 1074584
JVMDG217: Dump Handler is Processing OutOfMemory - Please Wait.
JVMDG315: JVM Requesting Heap dump file
JVMDG318: Heap dump file written to //heapdump536720.1608437021.phd
JVMDG303: JVM Requesting Java core file
JVMDG304: Java core file written to //javacore536720.1608437022.txt
JVMDG274: Dump Handler has Processed OutOfMemory.
JVMST109: Insufficient space in Javaheap to satisfy allocation request
Exception in thread "main" java.lang.OutOfMemoryError
at MemoryEater.main(MemoryEater.java(Compiled Code))
#
又一台
Total Memory = 2940271104 Free Memory = 2101680
Total Memory = 2940271104 Free Memory = 1090344
JVMDG217: Dump Handler is Processing OutOfMemory - Please Wait.
JVMDG315: JVM Requesting Heap dump file
JVMDG318: Heap dump file written to //heapdump26232102.1608436120.phd
JVMDG303: JVM Requesting Java core file
JVMDG304: Java core file written to //javacore26232102.1608436120.txt
JVMDG274: Dump Handler has Processed OutOfMemory.
JVMST109: Insufficient space in Javaheap to satisfy allocation request
Exception in thread "main" java.lang.OutOfMemoryError
at MemoryEater.main(MemoryEater.java(Compiled Code))
# oslevel -s
5300-06-06-0811
#
# /usr/java14/bin/java -version
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2)
Classic VM (build 1.4.2, J2RE 1.4.2 IBM AIX build ca142-20061124 (SR7) (JIT enab led: jitc))
又进行测试
Total Memory = 2940271104 Free Memory = 2122768 Allocated Memory = 2801MB
Total Memory = 2940271104 Free Memory = 1074176 Allocated Memory = 2802MB
JVMDG217: Dump Handler is Processing OutOfMemory - Please Wait.
JVMDG315: JVM Requesting Heap dump file
JVMDG318: Heap dump file written to //heapdump344230.1608548867.phd
JVMDG303: JVM Requesting Java core file
JVMDG304: Java core file written to //javacore344230.1608548868.txt
JVMDG274: Dump Handler has Processed OutOfMemory.
JVMST109: Insufficient space in Javaheap to satisfy allocation request
Exception in thread "main" java.lang.OutOfMemoryError
at MemoryEater.main(MemoryEater.java(Compiled Code))
#