Fundamentals 9 min read

How False Sharing Slows Java Programs and How Cache Line Padding Boosts Performance

This article explains the concept of false sharing in Java, demonstrates its dramatic impact on multithreaded performance, and shows how adding cache‑line padding variables can eliminate write‑contention, turning a 5‑to‑1 slowdown into a significant speedup.

Java Backend Technology
Java Backend Technology
Java Backend Technology
How False Sharing Slows Java Programs and How Cache Line Padding Boosts Performance

Before explaining the concept of false sharing , we run a program on a four‑core machine where four threads share an array and each repeatedly increments a distinct index one hundred million times, measuring the total execution time.

The program’s logic is four threads sharing the same array, each writing to a different element. After removing the commented padding code in SharingLong and rerunning, the execution time improves dramatically, showing a performance difference of up to 5 : 1.

False sharing (English: False Sharing ) occurs when variables that reside on the same cache line are accessed by different threads, causing unnecessary write‑contention. The padding lines in SharingLong are known as Cache Line Padding .

In a 64‑bit Java object, the header occupies two words (hashcode, flags, and pointer), and the fields v plus six p variables total eight long s, i.e., 64 bytes.

Modern CPUs have a three‑level cache hierarchy (L1, L2, L3). The smallest unit of cache storage is a cache line , typically 64 bytes. When a CPU reads from main memory, it loads an entire cache line; writes are first performed in the cache and later flushed back to memory.

The v field is declared volatile, which forces the CPU to make writes visible to other threads immediately, invalidating the corresponding cache line. If two volatile variables are placed adjacently, both threads may repeatedly load and invalidate the same cache line, leading to the false‑sharing effect.

Execution steps illustrate the problem:

CPU 1 reads v1 into its cache line.

CPU 2 reads v2 into its cache line.

CPU 1 writes v1, flushes to memory, and invalidates the line.

CPU 2 writes v2, sees the line invalidated, reloads it, and flushes again.

This interleaving causes repeated cache‑line loads—write competition—known as false sharing. Adding six padding long variables separates v1 and v2 into distinct cache lines, eliminating the contention and improving performance, albeit at the cost of lower cache‑line utilization (only ¼ of the line is used).

When the variables are changed from volatile to ordinary long s, the program runs three orders of magnitude faster because the CPU no longer needs to enforce immediate visibility, allowing most accesses to stay in cache.

Understanding false sharing is valuable for pure‑memory workloads; frameworks like the Disruptor use cache‑line padding to achieve order‑of‑magnitude throughput improvements over traditional Java queues.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

volatilefalse sharingJava concurrencycache line padding
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.