我想要测量运行基准测试时所消耗的内存。我在网上发现可以使用 GC
分析器来进行测量。我已经尝试了,但我不理解结果,也看不到所消耗的内存量。能否有人解释一下结果?谢谢。
MyBenchmark.testMethod_width_2_attribute_text ss 60 32.345 ± 1.759 ms/op
MyBenchmark.testMethod_width_2_attribute_text:·gc.alloc.rate ss 60 26.904 ± 0.217 MB/sec
MyBenchmark.testMethod_width_2_attribute_text:·gc.alloc.rate.norm ss 60 14999630.400 ± 12.578 B/op
MyBenchmark.testMethod_width_2_attribute_text:·gc.churn.PS_Eden_Space ss 60 28.282 ± 15.342 MB/sec
MyBenchmark.testMethod_width_2_attribute_text:·gc.churn.PS_Eden_Space.norm ss 60 15903402.667 ± 8631257.013 B/op
MyBenchmark.testMethod_width_2_attribute_text:·gc.churn.PS_Survivor_Space ss 60 0.654 ± 0.754 MB/sec
MyBenchmark.testMethod_width_2_attribute_text:·gc.churn.PS_Survivor_Space.norm ss 60 368914.667 ± 425374.152 B/op
MyBenchmark.testMethod_width_2_attribute_text:·gc.count ss 60 26.000 counts
MyBenchmark.testMethod_width_2_attribute_text:·gc.time ss 60 105.000 ms
PS_Eden_Space
中的PS代表什么?gc.count
是什么?垃圾回收器被调用的次数吗?gc.time
是什么?垃圾回收器在所有调用中所花费的总时间?count
和time
是在 JMH 迭代期间执行的垃圾回收次数和 JVM 在这些垃圾回收上花费的总时间。它们实际上是java.lang.management.GarbageCollectorMXBean
的getCollectionCount()
和getCollectionTime()
的迭代之间和迭代后的差异。您可以阅读这些方法的 Javadoc 以获取更多信息。 - undefined.norm
字段确实代表normalized
,并且对应于相关指标,但是除以jmh操作的数量。因此,如果您运行了一个调用了1,000次操作(即您的测试方法)的jmh迭代,那么例如·gc.alloc
(未报告)将是在该迭代期间分配的内存大小,·gc.alloc.rate
将是每单位时间(秒或毫秒)分配的内存量,而·gc.alloc.rate.norm
则是每个操作分配的内存量。最后一个指标应该能让您了解您的代码(即您的“操作”)分配了多少新的内存。 - undefined