1.代码一:
1. public class JVMTest {
2. public static void main(String args[]) throws InterruptedException {
3. byte [] bytes1 = new byte[1024*1024*2];
4. byte [] bytes2 = new byte[1024*1024*2];
5. byte [] bytes3 = new byte[1024*1024*4];
6. 2000);
7. byte [] bytes4 = new byte[1024*1024*4];
8. 2000);
9. }
10. }
执行:java -Xms20M -Xmx20M -Xmn10M -XX:+UseSerialGC -XX:+PrintHeapAtGC -Xloggc:gc.log JVMTest
gc两次:
1. {Heap before GC invocations=0 (full 0):
2. new generation total 9216K, used 5287K [7f9a00000, 7fa400000, 7fa400000)
3. 64% used [7f9a00000, 7f9f29c78, 7fa200000)
4. 0% used [7fa200000, 7fa200000, 7fa300000)
5. 0% used [7fa300000, 7fa300000, 7fa400000)
6. tenured generation total 10240K, used 0K [7fa400000, 7fae00000, 7fae00000)
7. 0% used [7fa400000, 7fa400000, 7fa400200, 7fae00000)
8. 800000000)
9. 22% used [7fae00000, 7fb2b3628, 7fb2b3800, 7fc2c0000)
10. No shared spaces configured.
11. 0.904: [GC 5287K->4447K(19456K), 0.0315833 secs]
12. Heap after GC invocations=1 (full 0):
13. new generation total 9216K, used 351K [7f9a00000, 7fa400000, 7fa400000)
14. 0% used [7f9a00000, 7f9a00000, 7fa200000)
15. 34% used [7fa300000, 7fa357ed8, 7fa400000)
16. 0% used [7fa200000, 7fa200000, 7fa300000)
17. tenured generation total 10240K, used 4096K [7fa400000, 7fae00000, 7fae00000)
18. 40% used [7fa400000, 7fa800020, 7fa800200, 7fae00000)
19. 800000000)
20. 22% used [7fae00000, 7fb2b3628, 7fb2b3800, 7fc2c0000)
21. No shared spaces configured.
22. }
23. {Heap before GC invocations=1 (full 0):
24. new generation total 9216K, used 4711K [7f9a00000, 7fa400000, 7fa400000)
25. 53% used [7f9a00000, 7f9e41eb0, 7fa200000)
26. 34% used [7fa300000, 7fa357ed8, 7fa400000)
27. 0% used [7fa200000, 7fa200000, 7fa300000)
28. tenured generation total 10240K, used 4096K [7fa400000, 7fae00000, 7fae00000)
29. 40% used [7fa400000, 7fa800020, 7fa800200, 7fae00000)
30. 800000000)
31. 22% used [7fae00000, 7fb2b4498, 7fb2b4600, 7fc2c0000)
32. No shared spaces configured.
33. 2.940: [GC 8807K->8537K(19456K), 0.0193472 secs]
34. Heap after GC invocations=2 (full 0):
35. new generation total 9216K, used 345K [7f9a00000, 7fa400000, 7fa400000)
36. 0% used [7f9a00000, 7f9a00000, 7fa200000)
37. 33% used [7fa200000, 7fa2566e8, 7fa300000)
38. 0% used [7fa300000, 7fa300000, 7fa400000)
39. tenured generation total 10240K, used 8192K [7fa400000, 7fae00000, 7fae00000)
40. 80% used [7fa400000, 7fac00030, 7fac00200, 7fae00000)
41. 800000000)
42. 22% used [7fae00000, 7fb2b4498, 7fb2b4600, 7fc2c0000)
43. No shared spaces configured.
44. }
2.代码2:
1. public class JVMTest {
2. public static void main(String args[]) throws InterruptedException {
3. byte [] bytes1 = new byte[1024*1024*2];
4. byte [] bytes2 = new byte[1024*1024*2];
5. byte [] bytes3 = new byte[1024*1024*2];
6. "step 1");
7. byte [] bytes4 = new byte[1024*1024*2];
8. 2000);
9. "step 2");
10. byte [] bytes5 = new byte[1024*1024*2];
11. byte [] bytes6 = new byte[1024*1024*2];
12. "step 3");
13. byte [] bytes7 = new byte[1024*1024*2];
14. 2000);
15. }
16. }
java -Xms20M -Xmx20M -Xmn10M -XX:+UseSerialGC -XX:+PrintHeapAtGC -Xloggc:gc.log JVMTest
1次minor gc 一次full gc:原因 minor gc晋升到旧生代的大小大于旧生代的剩余空间。
1. {Heap before GC invocations=0 (full 0):
2. new generation total 9216K, used 7335K [7f9a00000, 7fa400000, 7fa400000)
3. 89% used [7f9a00000, 7fa129c88, 7fa200000)
4. 0% used [7fa200000, 7fa200000, 7fa300000)
5. 0% used [7fa300000, 7fa300000, 7fa400000)
6. tenured generation total 10240K, used 0K [7fa400000, 7fae00000, 7fae00000)
7. 0% used [7fa400000, 7fa400000, 7fa400200, 7fae00000)
8. 800000000)
9. 22% used [7fae00000, 7fb2b38c0, 7fb2b3a00, 7fc2c0000)
10. No shared spaces configured.
11. 1.019: [GC 7335K->6495K(19456K), 0.0418591 secs]
12. Heap after GC invocations=1 (full 0):
13. new generation total 9216K, used 351K [7f9a00000, 7fa400000, 7fa400000)
14. 0% used [7f9a00000, 7f9a00000, 7fa200000)
15. 34% used [7fa300000, 7fa357ed8, 7fa400000)
16. 0% used [7fa200000, 7fa200000, 7fa300000)
17. tenured generation total 10240K, used 6144K [7fa400000, 7fae00000, 7fae00000)
18. 60% used [7fa400000, 7faa00030, 7faa00200, 7fae00000)
19. 800000000)
20. 22% used [7fae00000, 7fb2b38c0, 7fb2b3a00, 7fc2c0000)
21. No shared spaces configured.
22. }
23. {Heap before GC invocations=1 (full 0):
24. new generation total 9216K, used 6754K [7f9a00000, 7fa400000, 7fa400000)
25. 78% used [7f9a00000, 7fa040af0, 7fa200000)
26. 34% used [7fa300000, 7fa357ed8, 7fa400000)
27. 0% used [7fa200000, 7fa200000, 7fa300000)
28. tenured generation total 10240K, used 6144K [7fa400000, 7fae00000, 7fae00000)
29. 60% used [7fa400000, 7faa00030, 7faa00200, 7fae00000)
30. 800000000)
31. 22% used [7fae00000, 7fb2b47b0, 7fb2b4800, 7fc2c0000)
32. No shared spaces configured.
33. 3.073: [Full GC 12898K->12633K(19456K), 0.0712905 secs]
34. Heap after GC invocations=2 (full 1):
35. new generation total 9216K, used 4441K [7f9a00000, 7fa400000, 7fa400000)
36. 54% used [7f9a00000, 7f9e56680, 7fa200000)
37. 0% used [7fa300000, 7fa300000, 7fa400000)
38. 0% used [7fa200000, 7fa200000, 7fa300000)
39. tenured generation total 10240K, used 8192K [7fa400000, 7fae00000, 7fae00000)
40. 80% used [7fa400000, 7fac000c8, 7fac00200, 7fae00000)
41. 800000000)
42. 22% used [7fae00000, 7fb2b47b0, 7fb2b4800, 7fc2c0000)
43. No shared spaces configured.
44. }
新生代对象晋升到旧生代的规则:经过多次minor gc之后还存在的对象或者是to space放不下的直接晋升到old generation