为什么JVM需要预热? [英] Why does the JVM require warmup?

查看:1304
本文介绍了为什么JVM需要预热?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在Java虚拟机(JVM)中,当Java使用延迟加载过程加载类时,可能需要预热,因此您需要确保在启动主要事务之前初始化对象。我是一名C ++开发人员,无需处理类似的需求。



然而,我无法理解的部分如下:


  1. 您应该预热哪部分代码?

  2. 即使我预热了代码的某些部分,它仍然保持多久(假设这个术语仅表示类对象保留在内存中的时间)?

  3. 如果我每次收到事件时都需要创建对象,它有什么用处?

考虑一个预期通过套接字接收消息的应用程序的例子,这些事务可以是新订单,修改订单和取消订单或确认的交易。



请注意,该应用程序是关于高频交易(HFT),因此表现非常重要。

解决方案


您应该预热哪些代码部分?

通常,您不必做任何事情。但是对于低延迟应用,您应该预热系统中的关键路径。你应该进行单元测试,所以我建议你在启动时运行这些代码来加热代码。



即使您的代码被预热,您也必须确保CPU缓存也保持温暖。在阻止操作之后,您可以看到显着的性能下降,例如,网络IO,长达50微秒。通常情况下这不是问题,但如果你大部分时间都想保持在50微秒以下,这在大多数时候都会成为问题。



注意:热身可以让Escape Analysis踢出并放置一些物体到堆叠上。这意味着这些对象不需要进行优化。在优化代码之前,最好先对应用程序进行配置。

保持温暖(假设这个术语仅表示你的类对象在内存中保留了多久)?

没有时间限制。这取决于JIt是否检测到它在优化代码时所做的假设结果是不正确的。


如果我每次收到事件时都需要创建对象?

如果您想要低延迟或高性能,您应该创建尽可能少的物体。我的目标是生产速度低于300 KB /秒。有了这个分配率,你可以有一个足够大的伊甸园空间,可以每天轻微收集一次。


考虑一个预期应用程序通过套接字接收消息,交易可以是新订单,修改订单和取消订单或确认交易。


我建议您尽可能多的使用对象,但如果它在您的分配预算下,可能不值得担心。


请注意,应用程序是关于高频交易(HFT),因此表现非常重要。

您可能对我们使用的开源软件感兴趣对于不同投资银行和对冲基金的HFT系统。

http://chronicle.software/


我的制作应用程序用于高频交易,每一位延迟可能是一个问题。这很明显,在启动时如果你没有对应用程序进行热身,它会导致几毫秒的高延迟。

特别是您可能对 https://github.com/OpenHFT/Java-Thread-Affinity ,因为此库可以帮助减少关键线程中的调度抖动。


还有人说,需要预热的代码的关键部分应该以最少12K次的时间运行(使用假消息),以便以最佳方式工作。为什么以及它如何工作?

代码是使用后台线程编译的。这意味着即使某个方法可能符合编译为本地代码的条件,这并不意味着它在编译器已经非常繁忙时启动时尤其如此。 12K并非不合理,但可能会更高。

I understand that in the Java virtual machine (JVM), warmup is potentially required as Java loads classes using a lazy loading process and as such you want to ensure that the objects are initialized before you start the main transactions. I am a C++ developer and have not had to deal with similar requirements.

However, the parts I am not able to understand are the following:

  1. Which parts of the code should you warm up?
  2. Even if I warm up some parts of the code, how long does it remain warm (assuming this term only means how long your class objects remain in-memory)?
  3. How does it help if I have objects which need to be created each time I receive an event?

Consider for an example an application that is expected to receive messages over a socket and the transactions could be New Order, Modify Order and Cancel Order or transaction confirmed.

Note that the application is about High Frequency Trading (HFT) so performance is of extreme importance.

解决方案

Which parts of the code should you warm up?

Usually, you don't have to do anything. However for a low latency application, you should warmup the critical path in your system. You should have unit tests, so I suggest you run those on start up to warmup up the code.

Even once your code is warmed up, you have to ensure your CPU caches stay warm as well. You can see a significant slow down in performance after a blocking operation e.g. network IO, for up to 50 micro-seconds. Usually this is not a problem but if you are trying to stay under say 50 micro-seconds most of the time, this will be a problem most of the time.

Note: Warmup can allow Escape Analysis to kick in and place some objects on the stack. This means such objects don't need to be optimised away. It is better to memory profile your application before optimising your code.

Even if I warm up some parts of the code, how long does it remain warm (assuming this term only means how long your class objects remain in-memory)?

There is no time limit. It depends on whether the JIt detects whether the assumption it made when optimising the code turned out to be incorrect.

How does it help if I have objects which need to be created each time I receive an event?

If you want low latency, or high performance, you should create as little objects as possible. I aim to produce less than 300 KB/sec. With this allocation rate you can have an Eden space large enough to minor collect once a day.

Consider for an example an application that is expected to receive messages over a socket and the transactions could be New Order, Modify Order and Cancel Order or transaction confirmed.

I suggest you re-use objects as much as possible, though if it's under your allocation budget, it may not be worth worrying about.

Note that the application is about High Frequency Trading (HFT) so performance is of extreme importance.

You might be interested in our open source software which is used for HFT systems at different Investment Banks and Hedge Funds.

http://chronicle.software/

My production application is used for High frequency trading and every bit of latency can be an issue. It is kind of clear that at startup if you don't warmup your application, it will lead to high latency of few millis.

In particular you might be interested in https://github.com/OpenHFT/Java-Thread-Affinity as this library can help reduce scheduling jitter in your critical threads.

And also it is said that the critical sections of code which requires warmup should be ran (with fake messages) atleast 12K times for it to work in an optimized manner. Why and how does it work?

Code is compiled using background thread(s). This means that even though a method might be eligible for compiling to native code, it doesn't mean that it has done so esp on startup when the compiler is pretty busy already. 12K is not unreasonable, but it could be higher.

这篇关于为什么JVM需要预热?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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