Urgent.News

What's breaking now, across thousands of outlets.

Tech

Java Virtual Threads Aren't Free: 6 Hidden Costs to Check Before Production

Java virtual threads make it cheap to run a huge number of concurrent tasks, but they don't make your system's limits go away. Memory, database connections, CPU cores, locks and observability all still cost something. Virtual threads were previewed in Java 19 (JEP 425) and became a standard feature in Java 21 (JEP 444). Here are six costs worth checking before you switch a production service…

Java virtual threads make running numerous concurrent tasks seemingly inexpensive, but they do not eliminate underlying system limitations. Hidden costs include memory usage, database connections, CPU cores, locks, and observability. This report outlines six key costs to consider before deploying virtual threads in a production environment.

Virtual threads, introduced in Java 19, are lightweight threads managed by the JVM instead of the operating system. They allow for cost-effective scaling of thread-per-request code without the need for callbacks or reactive chains. However, they do not change the fundamental costs associated with the resources they interact with:

1. Memory consumption: Each virtual thread maintains its stack on the Java heap, which can quickly consume memory if each task holds significant data. Careful management of per-task objects and timely disposal of large buffers is crucial to prevent excessive heap growth and increased garbage collector workload.

2. Database connection limits: With virtual threads capable of handling thousands of concurrent requests, database connection pools may become the real bottleneck. If the pool size is insufficient, waiting requests may experience time-outs, leading to connection errors. It is essential to size the connection pool according to the database's capacity and set appropriate timeouts to fail fast when over capacity.

3. ThreadLocal caching inefficiency: ThreadLocal caching is applicable to virtual threads, but it becomes ineffective when used for caching expensive objects per thread. Due to the nature of virtual threads, a new thread is created for each task, rendering ThreadLocal caches redundant. Consider using alternatives such as DateTimeFormatter instead of SimpleDateFormat to avoid unnecessary memory overhead.

4. Pinning and carrier thread stalling: A virtual thread becomes pinned when it is unable to be unmounted from its carrier thread while blocked. This can lead to a reduction in throughput, especially on machines with limited CPU cores. To mitigate this issue, ensure that blocking calls are moved out of synchronized sections or replaced with ReentrantLock. On Java 21 to 23, monitoring the jdk.VirtualThreadPinned event with Java Flight Recorder can help identify potential pinning problems.

5. CPU-bound work limitations: While virtual threads can handle I/O-bound tasks efficiently, they do not contribute to faster CPU-bound work. They share the available CPU cores with a default ForkJoinPool, leading to reduced performance when handling computationally intensive tasks. For such workloads, it is advisable to use a fixed platform thread pool sized to the number of cores, or employ a fork-join approach for better scalability.

In conclusion, while Java virtual threads offer an attractive solution for handling numerous concurrent tasks, it is crucial to be aware of these hidden costs. Proper management of memory, database connections, thread caching, pinning, and workload type can help ensure optimal performance when deploying virtual threads in production environments.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Europe hit by another wave of grocery store freezer hacks

The new incidents come weeks after a cyberattack on fridges and freezers in France that forced supermarkets to throw away food.

  • Europe faces second wave of grocery store freezer hacks.
  • Swedish ICA and Coop report cooling issues at multiple stores.
  • Belgian Delhaize confirms IT incident affecting refrigeration systems.

More from Monday 2 November →