Java Performance Tuning
Java(TM) - see bottom of page
Our valued sponsors who help make this site possible
JProfiler: Get rid of your performance problems and memory leaks!
Training online: Concurrency, Threading, GC, Advanced Java and more ...
Tips August 2023
JProfiler
|
Get rid of your performance problems and memory leaks!
|
JProfiler
|
Get rid of your performance problems and memory leaks!
|
|
|
Back to newsletter 273 contents
https://www.youtube.com/watch?v=kb_cLmMAff4
Prepare for what "Loom"s ahead (Page last updated May 2023, Added 2023-08-29, Author Dr. Heinz Kabutz, Publisher Devoxx). Tips:
- Virtual threads let's you write many types of concurrent code without thread pools, which makes it easier to debug.
- Virtual threads are not suited to decomposing CPU heavy problems to smaller sub-problems then combining the results. These are better suited to ForkJoin. Virtual threads are well suited to tasks that can execute in parallel where the tasks need to wait for something else (eg IO)- the wait time is efficiently utilized by other virtual threads.
- Virtual threads are very low overhead threads, you can create and throw them away as much as you want.
- Virtual threads run on daemon (carrier/platform) threads in a ForkJoin pool. So be aware that if there are no non-daemon threads left, the daemon threads - and so all virtual threads - will be terminated.
- Properties jdk.virtualThreadScheduler.parallelism (default is Runtime.availableProcessors()) and jdk.virtualThreadScheduler.maxPoolSize (temporary increases for virtual threads waiting in synchronized blocks handled by ManagedBlocker) let's you set the number of platform threads available to run virtual threads.
- Executors.newVirtualThreadPerTaskExecutor() provides an autocloseable ExecutorService thread-pool object that doesn't actually make a pool, each task creates a new virtual thread, none are reused. They are low overhead pseudo-pools so you can create multiple ones. And you should be aware that if a thread from that pseudo-pool is interrupted while waiting for the ExecutorService to be shutdown (as would happen if exiting a try-with resource block) then shutdownNow() gets called.
- Virtual threads should minimize their task scope - do small chunks rather than large ones.
- Executors.newSingleThreadScheduledExecutor(Thread.ofVirtual().factory()) provides a scheduler that let's you have multiple timers on just one platform thread - otherwise each timer would run on a separate platform thread.
- Virtual threads should check if they have been interrupted, and terminate rather than ignore that.
- Virtual threads can synchronize, but you should avoid blocking in the synchronized block (that's generally advisable anyway). You can debug which threads are blocked while synchronized with the jdk.tracePinnedThreads property (set to full or short).
- Thread dumps no longer pause applications.
- Avoid using ThreadLocals with virtual threads, there will be a new Scoped Value available soon which is better suited for thread-local type capabilities.
- You can use IO classes (eg PrintStream, BufferedReader, etc) in virtual threads, but be aware that the IO objects tend to have large memory overheads, so creating too many can cause a large memory use.
https://devm.io/java/java-virtual-threads-pitfalls
Pitfalls to Avoid When Switching to Virtual Threads (Page last updated February 2023, Added 2023-08-29, Author Ram Lakshmanan, Publisher JAX Magazine). Tips:
- When using virtual threads, avoid synchronized blocks/methods, switch to concurrent locks if necessary. Otherwise the underlying platform thread is pinned for the duration of the block. The Java 21 implementation can spawn further platform threads for other virtual threads, but there is a limit to that and it's not efficient .
- When using virtual threads, avoid using thread pools to limit resource access. Thread pools using virtual threads are not really pools at all, they generate a new virtual thread for any submitted task. If you need to limit access to a resource (eg DB connections), use a different (pooling) mechanism.
- When using virtual threads, avoid ThreadLocals. You can easily generate a very high number of virtual threads over the application lifetime, and if each has their own copy of ThreadLocal variables, this can quickly fill up memory.
https://www.youtube.com/watch?v=SB3_jMzOxAw
What's Arriving for JFR in JDK 21 - Inside Java Newscast #53 (Page last updated July 2023, Added 2023-08-29, Author Billy Korando, Publisher Inside Java Newscast). Tips:
- jfr view is useful for looking at specific types of JFR events that you already know you want to focus on. If you don't know where you want to focus, you should first use jmc to view the various events .
- jfr view already has over 60 views.
- jfr view can be used on rcordings:
jfr view [view] [recording-file]
. It can also be used on a live JVM with no recording: jcmd [pid] JFR.view [view]
. Use maxage or maxsize to see more than the last 10 minutes or 32MB.
- New JFR events include jdk.JavaAgent, jdk.NativeAgent, jdk.ResidentSetSize, jdk.VirtualThreadStartEvent, jdk.VirtualThreadEndEvent, jdk.VirtualThreadPinnedEvent, jdk.VirtualThreadSubmitFailedEvent.
- A new JFR command flag option allows you to retain the recording: -XX:FlightRecirderOptions=preserve-repository=true/false.
Jack Shirazi
Back to newsletter 273 contents
Last Updated: 2024-10-29
Copyright © 2000-2024 Fasterj.com. All Rights Reserved.
All trademarks and registered trademarks appearing on JavaPerformanceTuning.com are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. JavaPerformanceTuning.com is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
URL: http://www.JavaPerformanceTuning.com/news/newtips273.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us