如何移除Java程序的垃圾回收时间?

huangapple go评论70阅读模式
英文:

How to remove java program's garbage collection time?

问题

我想对一个Java程序进行经验性时间复杂度分析。但是我该如何去除垃圾回收时间呢?

英文:

I wanna do empirical time complexity analysis to a java program. But how do I remove the garbage collection time?

答案1

得分: 1

你可以在Java 11及更高版本中使用Epsilon No-Op垃圾收集器来禁用垃圾收集器,该垃圾收集器是在『JEP 318: Epsilon: 一个无操作垃圾收集器(实验性)』中引入的。

在你的java命令中添加以下标志:
java -XX:+UseEpsilonGC ...

请确保你理解该垃圾收集器标志的限制(在链接的JEP中有解释)。有一个主要的注意事项:
> 一旦可用的Java堆耗尽,JVM将会关闭。

英文:

You can disable the garbage collector in Java 11+ using the Epsilon No-Op Garbage Collector, introduced in 'JEP 318: Epsilon: A No-Op Garbage Collector (Experimental)'.

Add the following flag to your java command:
java -XX:+UseEpsilonGC ...

Ensure you understand the limitations of this garbage collector flag (explained in the linked JEP). There is one major caveat:
> Once the available Java heap is exhausted, the JVM will shut down.

答案2

得分: 1

你可以尝试使用Epsilon GC来运行;参见@concision的答案

但是对于Java程序的经验复杂性应该包括GC开销。所以也许更好的方法是通过在同一JVM中重复运行程序并对时间求平均值来进行测量,以便分摊GC开销。考虑使用像JMH这样的基准测试框架。

英文:

You could try running with the Epsilon GC; see @concision's answer.

But the empirical complexity for a Java program should include the GC overheads. So maybe a better approach is to make your measurements by running your program repeatedly in the same JVM and averaging the times so that the GC overheads are amortized. Consider using a benchmarking framework like JMH.

答案3

得分: 1

看看其他直接回答你问题的答案,但通常情况下,如果你想检查性能特征,垃圾收集器只是可能影响你计时的五亿个因素之一。

使用JMH,它将尽量减少或彻底消除大多数这些因素,并且它将处理热点预热等待等问题。任何不基于JMH或类似工具的基准测试可能都不会有太大意义。

英文:

See the other answers which directly answer your question, but in general if you want to check performance characteristics, the garbage collector is just one of half a billion things that can mess with your timings.

Use JMH, which will try to mitigate or outright eliminate most of those things, and it will take care of waiting out hotspot warmup and the like. Any benchmark not based around JMH or similar is unlikely to mean much.

huangapple
  • 本文由 发表于 2020年10月5日 13:10:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64202771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定