在将最小和最大堆值设置为等于2之后,我只是在内存模拟中测试GC . ( - Xmx2m -Xms2m)
java程序应该将最大内存值设为2 MB . 但是在日志和堆转储分析器中看到它显示程序占用内存直到4 MB .
为什么它需要4 MB而不是2MB?
代码:
使用的VM标志:-Xmx2m -Xms2m -XX:PrintGCDetails -XX:PrintGCDateStamps -XX:HeapDumpOnOutOfMemoryError
public class GCFlags {
public static void main(String[] args) {
List<HumanBeing> humanBeings = new ArrayList<HumanBeing>();
System.out.println("Avaliable memory before while loop :"
+ Runtime.getRuntime().freeMemory() / 1024);
HumanBeing anjelinaJolie = null;
HumanBeing bradPitt = null;
while (true) {
anjelinaJolie = new HumanBeing("Anjelina Jolie");
bradPitt = new HumanBeing("Brad Pitt");
humanBeings.add(anjelinaJolie);
humanBeings.add(bradPitt);
}
}
}
public class HumanBeing {
private static String description = "Human being on earth";
private String name;
public HumanBeing(String name) {
super();
this.name = name;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name
* The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return description
*/
public String getDescription() {
return description;
}
}
控制台日志:
Avaliable memory before while loop :1709
2016-09-22T18:54:34.844+0500: [GC2016-09-22T18:54:34.844+0500: [DefNew: 845K->64K(960K), 0.0038254 secs] 845K->569K(1984K), 0.0039363 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2016-09-22T18:54:34.852+0500: [GC2016-09-22T18:54:34.852+0500: [DefNew: 802K->63K(960K), 0.0037686 secs]2016-09-22T18:54:34.856+0500: [Tenured: 1102K->1081K(1152K), 0.0122294 secs] 1307K->1081K(2112K), [Perm : 143K->143K(12288K)], 0.0160887 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
2016-09-22T18:54:34.876+0500: [GC2016-09-22T18:54:34.876+0500: [DefNew: 652K->64K(960K), 0.0026151 secs] 1733K->1730K(2764K), 0.0026988 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2016-09-22T18:54:34.881+0500: [GC2016-09-22T18:54:34.881+0500: [DefNew: 960K->64K(960K), 0.0035680 secs]2016-09-22T18:54:34.885+0500: [Tenured: 2561K->2162K(2608K), 0.0159277 secs] 2626K->2162K(3568K), [Perm : 143K->143K(12288K)], 0.0196062 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
2016-09-22T18:54:34.907+0500: [GC2016-09-22T18:54:34.907+0500: [DefNew: 1539K->128K(1728K), 0.0057813 secs] 3702K->3698K(5332K), 0.0058662 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2016-09-22T18:54:34.916+0500: [GC2016-09-22T18:54:34.916+0500: [DefNew: 1728K->1728K(1728K), 0.0000470 secs]2016-09-22T18:54:34.917+0500: [Tenured: 3570K->3604K(3604K), 0.0257361 secs] 5298K->4255K(5332K), [Perm : 143K->143K(12288K)], 0.0259492 secs] [Times: user=0.02 sys=0.00, real=0.03 secs]
2016-09-22T18:54:34.944+0500: [Full GC2016-09-22T18:54:34.944+0500: [Tenured: 3604K->4096K(4096K), 0.0281657 secs] **4854K->4844K(5952K),** [Perm : 143K->143K(12288K)], 0.0282632 secs] [Times: user=0.02 sys=0.00, real=0.03 secs]
2016-09-22T18:54:34.972+0500: [Full GC2016-09-22T18:54:34.972+0500: **[Tenured: 4096K->4095K(4096K), 0.0311708 secs]** 4844K->4834K(5952K), [Perm : 143K->143K(12288K)], 0.0312679 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid7888.hprof ...
Heap dump file created [9852670 bytes in 0.315 secs]
Heap
def new generation total 1856K, used 788K [0x34000000, 0x34200000, 0x34200000)
eden space 1664K, 47% used [0x34000000, 0x340c52b0, 0x341a0000)
from space 192K, 0% used [0x341a0000, 0x341a0000, 0x341d0000)
to space 192K, 0% used [0x341d0000, 0x341d0000, 0x34200000)
tenured generation total 4096K, used 4095K [0x34200000, 0x34600000, 0x34600000)
the space 4096K, 99% usedException in thread "main" [0x34200000, 0x345ffff8, 0x34600000, 0x34600000)
compacting perm gen total 12288K, used 146K [0x34600000, 0x35200000, 0x38600000)