gpt4 book ai didi

java - 为什么JVM需要预热?

转载 作者:IT老高 更新时间:2023-10-28 20:48:03 26 4
gpt4 key购买 nike

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

但是,我无法理解的部分如下:

  1. 您应该对代码的哪些部分进行预热?
  2. 即使我预热了代码的某些部分,它还能保持多长时间(假设这个术语仅表示您的类对象在内存中保留多长时间)?
  3. 如果我有需要在每次收到事件时创建的对象有什么帮助?

以一个应用程序为例,该应用程序期望通过套接字接收消息,并且交易可能是新订单、修改订单和取消订单或交易确认。

请注意,该应用程序涉及高频交易 (HFT),因此性能至关重要。

最佳答案

Which parts of the code should you warm up?

通常,您无需执行任何操作。但是,对于低延迟应用程序,您应该预热系统中的关键路径。你应该有单元测试,所以我建议你在启动时运行它们来预热代码。

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

注意:预热可以让 Escape Analysis 启动并将一些对象放在堆栈上。这意味着不需要优化此类对象。最好在优化代码之前对应用程序进行内存分析。

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)?

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

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

如果您想要低延迟或高性能,您应该创建尽可能少的对象。我的目标是产生低于 300 KB/秒的速度。使用此分配率,您可以拥有一个足够大的 Eden 空间,以便每天进行一次次要收集。

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.

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

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.

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

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?

代码是使用后台线程编译的。这意味着即使一个方法可能有资格编译为 native 代码,但这并不意味着它已经这样做了,尤其是在编译器已经很忙的启动时。 12K不是不合理,但可能会更高。

关于java - 为什么JVM需要预热?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36198278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com