Video in progress

Strong Middle ⚙️ JVM and memory

What was biased locking, and why was it disabled in JDK 15 and later removed completely?

Biased locking "bound" a lock to the first thread that took it, so reacquiring it didn't even need an atomic CAS. But revocation required a safepoint and complicated the code. Because its benefit shrank in modern applications, it was disabled in JDK 15 (JEP 374), and the code was removed in JDK 18.

📖 Answer

The idea was right back in the 2000s: synchronized collections like Vector, Hashtable and StringBuffer were everywhere, but almost always only one thread used them. A CAS on every call was a pointless cost. Biased locking wrote the thread ID into the mark word, and later synchronized calls became almost free.

The problems:

  • if another thread wanted that lock, the bias had to be revoked, which is a safepoint, meaning every thread is stopped. In applications working with thread pools this happened often;
  • the bulk rebias/revoke heuristics were among the most complicated parts of the HotSpot code and got in the way of other improvements;
  • modern code uses ArrayList, HashMap, ConcurrentHashMap, so uncontended synchronized became rarer, and CAS also got much cheaper on modern CPUs.

The versions: it was turned off by default and deprecated in JDK 15, and you could bring it back with -XX:+UseBiasedLocking. The code has been removed since JDK 18, and the flag no longer works.

The practical effect: when moving from Java 11 to 17/21, old code that calls synchronized a great many times from a single thread (for example StringBuffer, or an old library built on Vector) may get slightly slower. Usually you don't notice. If you do, the fix is to move the code to an unsynchronized analog. The upside: there are fewer safepoints, which is good for latency.