d: 1712K->1699K(10240K), 0.0071963 secs] 1712K->1699K(19456K), [Perm : 377K->372K(12288K)], 0.0072393 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 它显示了垃圾回收前老年代和永久区的内存占用量,以及垃圾回收后老年代和永久区的内存使用量。
并行收集器是工作在新生代的垃圾收集器,它只简单地将串行回收器多线程化。它的回收策略、算法以及参数和串行回收器一样。 并行回收器也是独占式的回收器,在收集过程中,应用程序会全部暂停。但由于并行回收器使用多线程进行垃圾回收,因此,在并发能力比较强的 CPU 上,它产生的停顿时间要短于串行回收器,而在单 CPU 或者并发能力较弱的系统中,并行回收器的效果不会比串行回收器好,由于多线程的压力,它的实际表现很可能比串行回收器差。 开启并行回收器可以使用参数-XX:+UseParNewGC,该参数设置新生代使用并行收集器,老年代使用串行收集器。
清单 5. 设置参数-XX:+UseParNewGC 的输出日志 [GC [ParNew: 825K->161K(4928K), 0.0155258 secs][Tenured: 8704K->661K(10944K), 0.0071964 secs] 9017K->661K(15872K), [Perm : 2049K->2049K(12288K)], 0.0228090 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] Heap par new generation total 4992K, used 179K [0x0f010000, 0x0f570000, 0x14560000) eden space 4480K, 4% used [0x0f010000, 0x0f03cda8, 0x0f470000) from space 512K, 0% used [0x0f470000, 0x0f470000, 0x0f4f0000) to space 512K, 0% used [0x0f4f0000, 0x0f4f0000, 0x0f570000) tenured generation total 10944K, used 8853K [0x14560000, 0x15010000, 0x1f010000) the space 10944K, 80% used [0x14560000, 0x14e057c0, 0x14e05800, 0x15010000) compacting perm gen total 12288K, used 2060K [0x1f010000, 0x1fc10000, 0x23010000) the space 12288K, 16% used [0x1f010000, 0x1f213228, 0x1f213400, 0x1fc10000) No shared spaces configured. 设置参数-XX:+UseConcMarkSweepGC 可以要求新生代使用并行收集器,老年代使用 CMS。
清单 6. 设置参数-XX:+UseConcMarkSweepGC 的输出日志 [GC [ParNew: 8967K->669K(14784K), 0.0040895 secs] 8967K->669K(63936K), 0.0043255 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Heap par new generation total 14784K, used 9389K [0x03f50000, 0x04f50000, 0x04f50000) eden space 13184K, 66% used [0x03f50000, 0x047d3e58, 0x04c30000) from space 1600K, 41% used [0x04dc0000, 0x04e67738, 0x04f50000) to space 1600K, 0% used [0x04c30000, 0x04c30000, 0x04dc0000) concurrent mark-sweep generation total 49152K, used 0K [0x04f50000, 0x07f50000, 0x09f50000) concurrent-mark-sweep perm gen total 12288K, used 2060K [0x09f50000, 0x0ab50000, 0x0df50000) 并行收集器工作时的线程数量可以使用-XX:ParallelGCThreads 参数指定。一般,最好与 CPU 数量相当,避免过多的线程数影响垃圾收集性能。在默认情况下,当 CPU 数量小于 8 个,ParallelGCThreads 的值等于 CPU 数量,大于 8 个,ParallelGCThreads 的值等于 3+[5*CPU_Count]/8]。以下测试显示了笔者笔记本上运行 8 个线程时耗时最短,本人笔记本是 8 核 IntelCPU。
清单 7. 设置为 8 个线程后 GC 输出 [GC [ParNew: 8967K->676K(14784K), 0.0036983 secs] 8967K->676K(63936K), 0.0037662 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Heap par new generation total 14784K, used 9395K [0x040e0000, 0x050e0000, 0x050e0000) eden space 13184K, 66% used [0x040e0000, 0x04963e58, 0x04dc0000) from space 1600K, 42% used [0x04f50000, 0x04ff9100, 0x050e0000) to space 1600K, 0% used [0x04dc0000, 0x04dc0000, 0x04f50000) concurrent mark-sweep generation total 49152K, used 0K [0x050e0000, 0x080e0000, 0x0a0e0000) concurrent-mark-sweep perm gen total 12288K, used 2060K [0x0a0e0000, 0x0ace0000, 0x0e0e0000) 清单 8. 设置为 128 个线程后 GC 输出 [GC [ParNew: 8967K->664K(14784K), 0.0207327 secs] 8967K->664K(63936K), 0.0208077 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 清单 9. 设置为 640 个线程后 GC 输出 [GC [ParNew: 8967K->667K(14784K), 0.2323704 secs] 8967K->667K(63936K), 0.2324778 secs] [Times: user=0.34 sys=0.02, real=0.23 secs] 清单 10. 设置为 1280 个线程后 GC 输出 Error occurred during initialization of VM Too small new size specified
新生代并行回收 (Parallel Scavenge) |