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 July 2021
JProfiler
|
Get rid of your performance problems and memory leaks!
|
JProfiler
|
Get rid of your performance problems and memory leaks!
|
|
|
Back to newsletter 248 contents
https://www.youtube.com/watch?v=nhYIEqt-jvY
Java Volatile(Page last updated September 2020, Added 2021-07-28, Author Jakob Jenkov, Publisher Jakob Jenkov). Tips:
- volatile means the variable must always be read from and written to main memory, not from a local (CPU) cache.
- Other variables even if not declared volatile that are visible at the same time as a volatile, will also need to be read from and written to main memory, but the instruction ordering can be reordered by the compiler which can impact what values are read to/written from main memory. Reordering cannot happen across the volatile variable.
- volatile is much slower than cached access, but eliminates some types of concurrency bugs. So only use volatile when it's needed.
- volatile is atomic only on the read and on the write, not on the combination. So for example the ++ operation is not atomic on a volatile as it is a read followed by a write. Two separate threads operating ++ on the same volatile variable will not necessarily increment the variable by 2.
https://www.opsian.com/blog/jvm-tlabs-important-multicore/
Why Java's TLABs are so important and why write contention is a performance killer in multicore environments(Page last updated July 2020, Added 2021-07-28, Author Sadiq Jaffer, Publisher opsian). Tips:
- Each JVM thread has blocks of the Java heap - termed TLABs (Thread-Local Allocation Buffers) - into which it can allocate. The TLABs are exclusive to one thread for allocation, but accessible by any thread. TLABs are designed to be CPU cache friendly - concurrent memory writes to the same location from multiple cores can involve cache lines being invalidated and changing state fairly rapidly. The term often used for this effect is called cache line ping-pong. By having only one thread able to update the TLAB, cache invalidation is reduced.
- When a thread runs out of space in its TLAB (Thread-Local Allocation Buffer) it requests a new buffer from the JVM's Garbage Collector. Within a TLAB, allocation is just a pointer increment.
- Shared state that is frequently written to by multiple threads should be avoided, as it fundamentally limits your scalability.
https://www.youtube.com/watch?v=BzpgKR1gZk4
Threading Safely in Java(Page last updated March 2021, Added 2021-07-28, Author Preslav Mihaylov, Publisher Java2Days). Tips:
- Thread safety is only an issue when you have shared mutable state. Stateless methods are thread-safe.
- synchronized introduces a lock for the the method or block, and all methods and blocks using the same object/lock will block more than one thread on all accesses and updates for that method/block.
- HashMap is not thread-safe. ConcurrentHashMap is and can be used as a swap in replacement with very little additional overhead.
- Doing two separate thread-safe actions does not result in a thread-safe compound action, because the compound action is not atomic so there is a race condition between the actions. Many thread-safe concurrent classes have atomic compound methods which can help you make a compound action thread-safe - like ConcurrentHashMap.putIfAbsent().
- You need to make both access and update actions together thread-safe, you can't just protect the updates.
Jack Shirazi
Back to newsletter 248 contents
Last Updated: 2025-01-27
Copyright © 2000-2025 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/newtips248.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us