关于垃圾收集器ParallelScavenge的GCTime
最近在看JVM垃圾收集相关的内容,发现了《深入理解java虚拟机》的描述不太容易理解;原文如下:
GCTimeRatio参数的值应当是一个大于0且小于100的整数,也就是垃圾收集时间占总时间的比率,相当于是吞吐量的倒数。如果把此参数设置为19,那允许的最大GC时间就占总时间的5(即1(119)),默认值为99,就是允许最大1(即1(199))的垃圾收集时间。
而书中吞吐量的公式为:吞吐量用户程序的运行时间(垃圾收集时间用户程序的运行时间);和上面提到的计算公式不匹配;所以感觉这里的描述是有问题的;后来查询官方文档:TheParallelCollector,官方关于GCTimeRatio的描述如下:
Throughput:Thethroughputgoalismeasuredintermsofthetimespentdoinggarbagecollectionversusthetimespentoutsideofgarbagecollection,referredtoasapplicationtime。ThegoalisspecifiedbythecommandlineoptionXX:GCTimeRatio,whichsetstheratioofgarbagecollectiontimetoapplicationtimeto1(1)
Forexample,XX:GCTimeRatio19setsagoalof120or5ofthetotaltimeingarbagecollection。Thedefaultvalueis99,resultinginagoalof1ofthetimeingarbagecollectionblockquote
描述中有一句话比较关键:whichsetstheratioofgarbagecollectiontimetoapplicationtimeto1(1);which指代GCTimeRatio参数,后面的描述说明可以通过这个参数计算垃圾收集时间占用应用程序时间的比例,公式为:1(1GCTimeRatio的值)。从字面上可以看出,GCTimeRatio的作用是为了计算垃圾收集的时间占用程序运行时间的值;而GCTimeRatio应该理解为用户程序的运行时间与垃圾收集时间的比例,即GCTimeRatiouserTimeGCTime,其中userTime表示用户程序运行时间,GCTime表示垃圾收集程序运营时间;
下面进行验证
当GCTimeRatio19时,我们通过GCTimeRatiouserTimeGCTime(对参数进行假设的公式)可以得到userTime19GCTime,再代入吞吐量计算公式:公式1:吞吐量用户程序的运行时间(垃圾收集时间用户程序的运行时间)
公式2:垃圾的运行时间(垃圾收集时间用户程序的运行时间)吞吐量1
代入公式1可得吞吐量userTime(GCTimeuserTime)19GCTime(GCTime19GCTime)19(119);
公式1的结果代入公式2,可得垃圾收集的运行时间(垃圾收集时间用户程序的运行时间)1吞吐量1〔19(119)〕1(119);和官方描述的公式一致;
总上所述:GCTimeRatio应该理解为用户程序的运行时间与垃圾收集时间的比例,即GCTimeRatiouserTimeGCTime,其中userTime表示用户程序运行时间,GCTime表示垃圾收集程序运营时间;