-XX:-PrintGC Print messages at garbage collection. Manageable. -XX:-PrintGC Details Print more details at garbage collection. Manageable. (Introduced in 1.4.0.) -XX:-PrintGCTimeStamps Print timestamps at garbage collection. Manageable (Introduced in 1.4.0.)
我们知道Java和C++的区别主要是,Java不需要像c++那样,由程序员主动的释放内存。而是由JVM里的GC(Garbage Collection)来,在适当的时候替我们释放内存。GC 的内部工作,即GC的算法有很多种, 如:标记清除收集器,压缩收集器,分代收集器等等。现在比较常用的是分代收集(也是SUN VM使用的),即将内存分为几个区域,将不同生命周期的对象放在不同区域里(新的对象会先 生成在Young area,在几次GC以后,如过没有收集到,就会逐渐升级到Tenured area)。在GC收集的时候,频繁收集生命周期短的区域(Young area),因为这个区域内的对象生命周期比较短,GC 效率也会比较高。而比较少的收集生命周期比较长的区域(Old area or Tenured area),以及基本不收集的永久区(Perm area)。 注:Young area又分为三个区域分别叫Eden,和俩个Survivor spaces。Eden用来存放新的对象,Survivor spaces用于 新对象 升级到 Tenured area时的 拷贝。 我们管收集 生命周期短的区域(Young area) 的收集叫 GC,而管收集 生命周期比较长的区域(Old area or Tenured area)的收集叫 Full GC,因为他们的收集算法不同,所以使用的时间也会不同。我们要尽量减少 Full GC 的次数。
接下来介绍一下 HotSpot VM GC 的种类,GC在 HotSpot VM 5.0里有四种。一种是默认的叫 serial collector,另外几种分别叫throughput collector,concurrent low pause collector, incremental (sometimes called train) low pause collector(废弃掉了)。以下是SUN的官方说明:
1. The throughput collector: this collector uses a parallel version of the young generation collector. It is used if the -XX:+UseParallelGC option is passed on the command line. The tenured generation collector is the same as the serial collector. 2. The concurrent low pause collector: this collector is used if the -Xincgc? or -XX:+UseConcMarkSweepGC is passed on the command line. The concurrent collector is used to collect the tenured generation and does most of the collection concurrently with the execution of the application. The application is paused for short periods during the collection. A parallel version of the young generation copying collector is used with the concurrent collector. The concurrent low pause collector is used if the option -XX:+UseConcMarkSweepGC is passed on the command line. 3. The incremental (sometimes called train) low pause collector: this collector is used only if -XX:+UseTrainGC is passed on the command line. This collector has not changed since the J2SE Platform version 1.4.2 and is currently not under active development. It will not be supported in future releases. Please see the 1.4.2 GC Tuning Document for information on this collector.
* stop all application threads; do the initial mark; resume all application threads(第一次暂停,初始话标记) * do the concurrent mark (uses one procesor for the concurrent work)(运行是标记) * do the concurrent pre-clean (uses one processor for the concurrent work)(准备清理) * stop all application threads; do the remark; resume all application threads(第二次暂停,标记,检查) * do the concurrent sweep (uses one processor for the concurrent work)(运行过程中清理) * do the concurrent reset (uses one processor for the concurrent work)(复原)
当要使用Incremental mode时,需要使用以下几个变量: -XX:+CMSIncrementalMode default: disabled 启动i-CMS模式(must with -XX:+UseConcMarkSweepGC) -XX:+CMSIncrementalPacing default: disabled 提供自动校正功能 -XX:CMSIncrementalDutyCycle= default: 50 启动CMS的上线 -XX:CMSIncrementalDutyCycleMin= default: 10 启动CMS的下线 -XX:CMSIncrementalSafetyFactor= default: 10 用来计算循环次数 -XX:CMSIncrementalOffset= default: 0 最小循环次数(This is the percentage (0-100) by which the incremental mode duty cycle is shifted to the right within the period between minor collections.) -XX:CMSExpAvgFactor= default: 25 提供一个指导收集数