如何在JVM启动时预加载已使用的类?

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

How to preload used class when JVM starts?

问题

我正在使用Java开发一个消息系统,需要低延迟处理(在1秒以内)。然而,JVM需要热身时间来处理第一个输入数据,这会导致延迟增加(约2~3秒)。导致延迟增加的主要原因是类加载。我知道最简单的解决方案是使用虚拟消息提前调用方法。然而,由于系统要求,我不能使用虚拟消息来预热JVM。因此,我想知道在JVM启动时预加载所有使用的类的方法。

我尝试了通过以下属性来强制加载方法:

-XX:CompileThreshold=0 -XX:TieredCompilation

然而,似乎效果不佳。JVM仍然会在调用它们时加载类。

我还阅读了其他帖子,但没有人具体说明了在JVM启动时预加载类的方法。

链接:

https://stackoverflow.com/questions/677739/preloading-java-classes-libraries-at-jar-startup/677994#677994

https://stackoverflow.com/questions/1481853/technique-or-utility-to-minimize-java-warm-up-time

英文:

I am developing a messaging system with Java that requires low latency processing (under 1 second). However, JVM takes warm-up time to process the first input data that causes latency increase (about 2~3 seconds). The main reason of the latency increase was class loading. I know the simplest solution is to use dummy messages to invoke methods in advance. However, I cannot use dummy messages to warm-up JVMs because of the system requirement. So I want to know the method to preload all the used classes when the JVM starts.

I tried the properties to force-load methods by

-XX:CompileThreshold=0 -XX:TieredCompilation

However, it doesn't seem to be working well. The JVM still loads classes when they are called.

I also read other threads, but no one specified the method to preload classes when a JVM starts.

https://stackoverflow.com/questions/677739/preloading-java-classes-libraries-at-jar-startup/677994#677994

https://stackoverflow.com/questions/1481853/technique-or-utility-to-minimize-java-warm-up-time

答案1

得分: 1

我认为你需要使用提前编译(AOT)的Java编译器,将类编译为可以由JVM加载的本机代码。

一个例子是在Java 9中引入的jaotc工具,作为JEP 295工作的一部分。它接受一系列类(先前编译为字节码文件),并将它们编译为本机代码库;例如Linux的.so文件。然后,您可以在命令行中告诉JVM有关.so文件,它将加载AOT编译的代码并使用它。

AOT编译的好处是更快的JVM启动时间。不足之处在于AOT编译器无法像JIT编译器一样进行优化,因此长时间运行的程序在整体性能方面会受到影响。

因此,您(显然)需要满足快速启动的要求,这可能导致您无法满足长期吞吐量要求。

其他参考资料:

英文:

I think you need to be using an ahead-of-time (AOT) Java compiler that compiles classes to native code that can be loaded by the JVM.

One example is the jaotc tool that was introduced in Java 9 as a result of the JEP 295 work. It takes a list of classes (previously compiled to bytecode files) and compiles them to a native code library; e.g. a Linux .so file. You can then tell the JVM about the .so file on the command line, and it will load the AOT compiled code and use it.

The upside of AOT compilation is faster JVM startup. The downside is that an AOT compiler can't do as good a job of optimizing as a JIT compiler, so overall performance suffers in a long running program.

So your (apparent) need to meet a fast startup requirement may lead to you not meeting a long-term throughput requirement.

Additional references:

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

发表评论

匿名网友

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

确定