Concurrency
False Sharing and the Cache Line You Share
Eight threads, eight private slots, nothing shared in the program's terms - and 2.30 times the throughput once they stopped sharing a cache line.

On this page
Eight threads, eight distinct array slots, and no two threads ever touching the same slot. Packed into one cache line they managed 2,010,309,499 ops/s. Given a line each, 4,619,967,081.
Nothing is shared in the program’s terms. There is no shared variable, no lock, no atomic, and no data race to fix. The 2.30x is paid by the cache coherence protocol on behalf of code that, read as Java, has no contention in it at all.
The machine: Apple M4 Pro, eight performance and four efficiency cores, hw.cachelinesize 128. JDK 26.0.2.1 (Homebrew build), macOS 26.6.2, JMH 1.37 with six forks and five warmup plus five measurement iterations — 30 measurements per row.
The curve is threads per line, not distance
Each of eight threads increments its own slot in a shared long[], at index threadIndex * offset. Nothing else changes between the rows.
| offset | Bytes apart | Writers per 128-byte line | ops/s | Error |
|---|---|---|---|---|
| 1 | 8 | 8 | 2,010,309,499 | 16.6% |
| 4 | 32 | 4 | 3,611,525,517 | 12.7% |
| 8 | 64 | 2 | 4,373,832,089 | 7.3% |
| 16 | 128 | 1 | 4,619,967,081 | 1.4% |
Read as distance the curve is odd — doubling the gap from 8 to 16 bytes buys far more than doubling it from 64 to 128. Read as the third column it is not odd at all: 8, 4, 2 and 1 writers per line, with throughput rising as the number of cores fighting over each line falls, and flattening once each writer owns one. Distance is not the quantity; it is a proxy for the quantity, and the proxy is only as good as the line width you assumed.
The measurement itself needs one piece of care. A plain array increment is hoistable out of JMH’s loop, and a volatile one adds a fence whose cost would be read as contention. Both would produce a confident number for something other than the effect. Opaque access — VarHandle.getOpaque and setOpaque — forbids the first and buys none of the second, which is the kind of choice a benchmark has to make before it means anything.
The number to pad to is a machine property
The figure in circulation is 64 bytes. It is in the reference most readers reach first: “A cache line typically consists of 64 bytes.” That is true of a great deal of hardware and it is not true here.
Two sources on this machine agree on 128. The operating system reports hw.cachelinesize: 128. The JVM’s own answer is ContendedPaddingWidth, which defaults to 128 — the width HotSpot pads with when a field carries @Contended, itself gated behind RestrictContended, which defaults to true.
That is the whole of the practical advice, and it is one command rather than a constant: ask the machine that will run the code. A padding width copied from an article is a guess about somebody else’s hardware.
What this run cannot tell you
Spacing the writers 64 bytes apart — the number the articles give — measured 4,373,832,089 ops/s, or 2.18x the packed baseline, against 2.30x for a full line each. The intervals overlap: 4.05–4.69 billion against 4.56–4.68 billion. On this evidence 64 bytes cannot be called insufficient.
It also cannot be called equivalent, and the reason is in the third column rather than in the statistics. At 64-byte spacing on a 128-byte line there are still two writers per line. The arrangement that overlaps with full separation is not the same experiment as full separation; it is a different point on the same curve that happens to sit close to it. Publishing that as “64 bytes was enough” would be reporting a coincidence of this workload’s thread count and access pattern as a property of the platform.
The variance is the earlier signal
The error column moves further than the mean does, and it moves first: 16.6 per cent of the score under full contention, then 12.7, 7.3, and 1.4 once each writer owns a line. Same benchmark, same fixed work per operation, same number of threads.
That is useful because it is visible without knowing to look for false sharing. A benchmark whose work per operation is constant and whose interval is wide is reporting that something outside the code is varying between runs, and coherence traffic across cores is one of the few things that varies that much. On this evidence the interval was the more sensitive instrument: it separated the four arrangements more cleanly than the means did at the crowded end of the curve.
Measuring instead of padding
The sequence is short. Read the line width from the machine. Space the writers and watch the throughput curve — if it does not move, false sharing was not the problem and padding will cost footprint for nothing. If it does move, the useful quantity is writers per line, and the fix is whatever arrangement gets that to one: padding, per-thread accumulators combined at the end, or simply not putting two hot counters in the same object.
What none of it produces is a number to copy. The first version of this experiment used two threads and average time per operation, and it could not separate the arrangements at all — the effect was there and the harness could not see it. The measurement had to be designed before the mechanism was visible, which is the ordinary case rather than the exception.
Frequently asked
- How much padding do I need to avoid false sharing?
- That is a property of the machine, not of Java. This one reports a 128-byte line via hw.cachelinesize, and the JVM's ContendedPaddingWidth default matches it at 128. The widely repeated 64 comes from hardware where the line is 64 bytes. Read the value on the machine that will run the code.
- Can I see false sharing without a profiler?
- In the run below it showed up in the error bars before it showed up in the mean. Full contention produced a score interval of 16.6 per cent; one writer per line produced 1.4 per cent on the same benchmark. A wide interval on a benchmark whose work is fixed is worth investigating as contention.


