计算单元测试运行期间发生的gc数量 [英] Count number of gc's that occur during a unit test run

查看:117
本文介绍了计算单元测试运行期间发生的gc数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写单元测试以查看给定方法的性能影响。从实践中我们观察到,在执行给定方法的过程中,当前有很多gc正在发生。我想知道是否可以看到在从java运行方法期间发生了多少gcs。

可以使用 GarbageCollectorMXBean 来获取垃圾收集的数量。



您可以这样做:

 集合< GarbageCollectorMXBean> garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans(); (GarbageCollectorMXBean garbageCollectorMBean:garbageCollectors)
{
String gcName = garbageCollectorMBean.getName();
long gcCount = garbageCollectorMBean.getCollectionCount();
}


I am currently writing a unit test to see the performance impact of a given method. From practice we observed that currently lots of gc's are occurring during the execution of the given method. I was wondering whether it is possible to see how many gcs occurred during the method run from java.

解决方案

You can use GarbageCollectorMXBean to get the count of garbage collections.

You can do something like this:

Collection<GarbageCollectorMXBean> garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean garbageCollectorMBean : garbageCollectors) {
    String gcName = garbageCollectorMBean.getName();
    long gcCount = garbageCollectorMBean.getCollectionCount();
}

这篇关于计算单元测试运行期间发生的gc数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆