您的 Java 低延迟应用程序开发清单是什么?

发布于 2024-08-27 19:39:43 字数 363 浏览 9 评论 0原文

我想为 Java 低延迟应用程序创建全面的清单。您可以在此处添加您的清单吗?

这是我的清单
1. 使你的对象不可变
2.尽量减少synchronized方法
3. 锁定顺序应有据可查,并谨慎处理
4. 使用分析器
5.利用Amdhal定律,找到顺序执行路径
6. 使用 Java 5 并发实用程序和锁
7. 避免线程优先级,因为它们依赖于平台
8.可以使用JVM预热
9. 更喜欢不公平的锁定策略
10.避免上下文切换(多线程会导致适得其反)
11.避免装箱、拆箱
12.注意编译器警告
13. 线程数应等于或小于核心数

低延迟应用程序每毫秒进行调整。

I would like to create comprehensive checklist for Java low latency application. Can you add your checklist here?

Here is my list
1. Make your objects immutable
2. Try to reduce synchronized method
3. Locking order should be well documented, and handled carefully
4. Use profiler
5. Use Amdhal's law, and find the sequential execution path
6. Use Java 5 concurrency utilities, and locks
7. Avoid Thread priorities as they are platform dependent
8. JVM warmup can be used
9. Prefer unfair locking strategy
10. Avoid context-switching (many threads lead to counter productive)
11. Avoid boxing, un-boxing
12. Give attention to compiler warnings
13. Number of threads should be equal or lesser than the number of core

Low-latency application is tuned for every milli-seconds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

难以启齿的温柔 2024-09-03 19:39:43

尽管不变性很好,但它并不一定会改善延迟。确保低延迟可能取决于平台。

除了一般性能之外,GC 调优也非常重要。减少内存使用将有助于GC。特别是如果您可以减少需要移动的中年对象的数量 - 使其对象要么长期存在,要么短期存在。还要避免任何东西接触烫发根。

Although immutability is good, it is not necessarily going to improve latency. Ensuring low-latency is likely to be platform dependent.

Other than general performance, GC tuning is very important. Reducing memory usage will help GC. In particular if you can reduce the number of middle-aged objects that need to get moved about - keep it object either long lived or short lived. Also avoid anything touching the perm gen.

×眷恋的温暖 2024-09-03 19:39:43

避免装箱/拆箱,如果可能的话使用原始变量。

avoid boxing/unboxing, use primitive variables if possible.

与往事干杯 2024-09-03 19:39:43

在消息处理路径上尽可能避免上下文切换
结果:使用NIO和单事件循环线程(反应堆)

Avoid context switching wherever possible on the message processing path
Consequence: use NIO and single event loop thread (reactor)

幸福%小乖 2024-09-03 19:39:43

购买、阅读并理解Effective Java。另外在线提供

Buy, read, and understand Effective Java. Also available online

筑梦 2024-09-03 19:39:43

避免大量锁定和多线程,以免破坏现代处理器(及其缓存)的增强功能。然后,您可以使用单个线程达到令人难以置信的极限(每秒 600 万个事务),并且延迟非常低。

如果您想了解真实世界的低延迟 Java 应用程序及其架构的足够详细信息,请查看 LMAX:

LMAX架构

Avoid extensive locking and multi-threading in order not to disrupt the enhanced features in modern processors (and their caches). Then you can use a single thread up to its unbelievable limits (6 million transactions per second) with very low latency.

If you want to see a real world low-latency Java application with enough details about its architecture have a look at LMAX:

The LMAX Architecture

一口甜 2024-09-03 19:39:43

测量,测量,再测量。使用尽可能接近实际的数据和尽可能接近生产的硬件来定期运行基准测试。低延迟应用程序通常最好被视为设备,因此您需要考虑部署的整个盒子,而不仅仅是特定的方法/类/包/应用程序/JVM 等。如果您不在生产环境中构建现实的基准(例如设置),您将会感到惊讶生产。

Measure, measure and measure. Use as close to real data with as close to production hardware to run benchmarks regularly. Low latency applications are often better considered as appliances, so you need to consider the whole box deployed not just the particular method/class/package/application/JVM etc. If you do not build realistic benchmarks on production like settings you will have surprises in production.

昔梦 2024-09-03 19:39:43
  • 考虑使用非阻塞方法而不是同步。
  • 考虑使用易失性或原子变量而不是阻塞数据结构和锁。
  • 考虑使用对象池。
  • 使用数组而不是列表,因为它们对缓存更友好。
  • 通常,对于小型任务,由于锁定以及内存和缓存访问延迟,将数据发送到其他核心可能比在单个核心上处理花费更多的时间。因此,请考虑通过单个线程处理任务。
  • 减少访问主内存的频率并尝试使用缓存中存储的数据。
  • 考虑选择服务器端 C2 JIT 编译器,它专注于性能优化,而 C1 则专注于快速启动时间。
  • 当不同线程使用的两个字段可以位于单个缓存行上时,请确保没有错误的对象字段共享。
  • 阅读 https://mechanical-sympathy.blogspot.com/
  • 考虑使用 UDP over TCP
  • Consider using non-blocking approaches rather than synchronisation.
  • Consider using volatile or atomic variables over blocking data structures and locks.
  • Consider using object pools.
  • Use arrays instead of lists as they are more cache-friendly.
  • Normally for small tasks sending data to other cores can take more time than processing on a single core because of locking and memory and cache access latency. Hence, consider processing a task by a single thread.
  • Decrease the frequency of accessing main memory and try to work with data stored in caches.
  • Consider choosing a server-side C2 JIT compiler that is focused on performance optimizations contrary to C1 which is focused on quick startup time.
  • Make sure you don't have false object field sharing when two fields used by different threads can be situated on a single cache line.
  • Read https://mechanical-sympathy.blogspot.com/
  • Consider using UDP over TCP
情话墙 2024-09-03 19:39:43

不要在应用程序中安排比底层硬件上的内核数更多的线程。请记住,操作系统将需要线程执行,并且可能需要共享相同硬件的其他服务,因此您的应用程序可能需要使用少于可用核心的最大数量。

Do not schedule more threads in your application than you have cores on the underlying hardware. Keep in mind that the OS will require thread execution and potentially other services sharing the same hardware, so your application may be requried to use less than the maximunm number of cores available.

Oo萌小芽oO 2024-09-03 19:39:43

我认为“仅在适当的情况下使用可变对象”比“使对象不可变”更好。许多延迟极低的应用程序都有重复使用的对象池,以最大限度地减少 GC。不可变对象不能以这种方式重用。例如,如果您有一个 Location 类:

class Location {
    double lat;
    double lon;
}

您可以在启动时创建一些类并一遍又一遍地使用它们,这样它们就不会导致分配和后续 GC。

不过,这种方法比使用不可变位置对象要棘手得多,因此应该只在需要的地方使用它。

I think "Use mutable objects only where appropriate" is better than "Make your objects immutable". Many very low latency applications have pools of objects they reuse to minimize GC. Immutable objects can't be reused in that way. For example, if you have a Location class:

class Location {
    double lat;
    double lon;
}

You can create some on bootup and use them over and over again so they never cause allocations and the subsequent GC.

This approach is much trickier than using an immutable location object though, so it should only be used where needed.

初懵 2024-09-03 19:39:43

生成大型字符串时,请使用 StringBuilder 而不是 String。例如查询。

Use StringBuilder instead of String when generating large Strings. For example queries.

梦里梦着梦中梦 2024-09-03 19:39:43

另一个重要的想法是首先让它工作,然后测量性能,然后隔离所有瓶颈,然后优化它们,然后再次测量以验证改进。

正如 Knuth 所说,“过早的优化是万恶之源”。

Another important idea is to get it working first, then measure the performance, then isolate any bottlenecks, then optimize them, then measure again to verify improvement.

As Knuth said, "premature optimization is the root of all evil".

指尖上得阳光 2024-09-03 19:39:43

除了此处建议的开发人员级解决方案之外,考虑加速 JIT 运行时(例如 Zing)和堆外内存解决方案(如 Teracotta BigMemory、Apache Ignite)以减少 Stop-the-world GC 暂停也非常有益。
如果某些 GUI 涉及使用二进制协议(如 Hessian)、ZERO-C ICE 而不是 Web 服务等,则非常有效。

In addition to developer level solutions advised here already it can also be very beneficial to consider accelerated JIT runtimes e.g Zing and off heap memory solutions like Teracotta BigMemory, Apache Ignite to reduce Stop-the-world GC pauses.
If some GUI involved using Binary Protocols like Hessian, ZERO-C ICE instead of webservice etc is very effective.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文