最小化 Java“预热"的技术或实用程序时间? [英] Technique or utility to minimize Java "warm-up" time?

查看:21
本文介绍了最小化 Java“预热"的技术或实用程序时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在支持一个需要低延迟(<300 微秒处理每条消息)的 Java 消息传递应用程序.但是,我们的分析表明 Sun Java 虚拟机起初运行缓慢,在前 5,000 条左右的消息之后加速.前 5,000 条消息的延迟为 1-4 毫秒.大约前 5,000 条之后,后续消息有大约 250 微秒的延迟,偶尔会出现异常值.

I am supporting a Java messaging application that requires low latency (< 300 microseconds processing each message). However, our profiling shows that the Sun Java Virtual Machine runs slowly at first, and speeds up after the first 5,000 messages or so. The first 5,000 messages have latency of 1-4 milliseconds. After about the first 5,000, subsequent messages have ~250 microseconds latency, with occasional outliers.

人们普遍认为这是 Java 应用程序的典型行为.但是,从业务角度来看,告诉客户他们必须等待 JVM预热"才能看到他们需要的性能是不可接受的.在处理第一条客户消息之前,应用程序需要预热"

It's generally understood that this is typical behavior for a Java application. However, from a business standpoint it's not acceptable to tell the customer that they have to wait for the JVM to "warm-up" before they see the performance they demand. The application needs to be "warmed-up" before the first customer message is processed

JVM 是 Sun 1.6.0 update 4.

The JVM is Sun 1.6.0 update 4.

克服这个问题的想法:

  1. JVM 设置,例如 -XX:CompileThreshold=
  2. 添加一个组件以在启动时预热"应用程序,例如通过应用程序发送假消息".
  3. 在应用程序启动时静态加载应用程序和 JDK 类,以便在处理客户消息时不会从 JAR 加载类.
  4. 一些实用程序或 Java 代理可以实现上述两个想法中的一个或两个,这样我就不必重新发明轮子了.

注意:显然,对于这个解决方案,我正在考虑所有因素,包括芯片架构、磁盘类型和配置以及操作系统设置.但是,对于这个问题,我想重点讨论如何优化 Java 应用程序并最大限度地减少预热"时间.

NOTE: Obviously for this solution I'm looking at all factors, including chip arch, disk type and configuration and OS settings. However, for this question I want to focus on what can be done to optimize the Java application and minimize "warm up" time.

推荐答案

Java 中的预热"通常是关于两件事:

"Warm-up" in Java is generally about two things:

(1): 延迟类加载:这可以通过强制加载来解决.

(1): Lazy class loading: This can be work around by force it to load.

最简单的方法是发送假消息.您应该确保假消息将触发对类的所有访问.例如,如果您发送一条空消息,但您的程序会检查消息是否为空并避免执行某些操作,那么这将不起作用.

The easy way to do that is to send a fake message. You should be sure that the fake message will trigger all access to classes. For exmaple, if you send an empty message but your progrom will check if the message is empty and avoid doing certain things, then this will not work.

另一种方法是通过在程序启动时访问该类来强制类初始化.

Another way to do it is to force class initialization by accessing that class when you program starts.

(2):实时优化:在运行时,Java VM会优化部分代码.这是有预热时间的主要原因.

(2): The realtime optimization: At run time, Java VM will optimize some part of the code. This is the major reason why there is a warm-up time at all.

为了缓解这种情况,您可以发送一堆虚假(但看起来是真实的)消息,以便优化可以在您的用户使用之前完成.

To ease this, you can sent bunch of fake (but look real) messages so that the optimization can finish before your user use it.

您可以帮助缓解此问题的另一个方法是支持内联,例如尽可能多地使用 private 和 final.原因是,VM不需要查找继承表来查看实际调用什么方法.

Another that you can help to ease this is to support inline such as using private and final as much as you can. the reason is that, the VM does not need to look up the inheritance table to see what method to actually be called.

希望这会有所帮助.

这篇关于最小化 Java“预热"的技术或实用程序时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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