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.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You can use GarbageCollectorMXBean to get the count of garbage collections.
You can do something like this:
Can't you simply call this method with the parameters and count them?
Point 1
If you have GC calls during your method you will get wrong results. All GC calls are Stop The World calls for some phases of the algorithm, this meaning that your Threads are put on hold for that particular time. For how much it depends on the application that you are running, the heap you are giving to the JVM and the GC algorithm that you choose for the young and old generation. Note that calls to young generation are way cheaper then those in the old generation.
Point 2
Benchmarking in Java is complicated, mainly because of the JIT. You should use a library for your tests (if you can). One very good is JMH. If you can't use one, then let the jvm heat up a bit before running the actual benchmark, like 10000 times running the same method then measuring is usually the recommended.