Quick revision
The short answer
A cache line is the fixed-size block, usually 64 bytes, in which the CPU moves memory between RAM and its caches. False sharing happens when threads on different cores write to different variables that happen to sit in the same cache line: the cache coherence protocol treats the whole line as shared, so each write invalidates the other core's copy and the line bounces between cores, making writes far slower than they should be.
What to remember
In this answer, we saw that CPUs cache memory in fixed blocks called cache lines, commonly 64 bytes, and that a coherence protocol forces a core to own a line exclusively before writing to it. When threads on different cores write to different variables in the same line, the line keeps bouncing between them, which is false sharing. It typically appears with counters, flags and indexes stored next to each other, and we fix it by giving each hot variable its own line after confirming the problem with a benchmark.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A cache line is the fixed-size block, usually 64 bytes, in which the CPU moves memory between RAM and its caches. False sharing happens when threads on different cores write to different variables that happen to sit in the same cache line: the cache coherence protocol treats the whole line as shared, so each write invalidates the other core's copy and the line bounces between cores, making writes far slower than they should be.
Why this question is asked
False sharing is one of those problems that is invisible in the source code. Two threads touch two different fields, there is no lock and no shared data, and still the program gets slower when we add the second thread. Interviewers ask about it because it tests whether we understand that the hardware works in blocks, not in variables. In this answer, we will recap how CPU caches store memory, see how cores keep their caches consistent, explain why that makes unrelated variables interfere, and look at where false sharing usually appears in Java code.
1A Quick Recap of CPU Caches
Main memory is slow compared with a CPU core. Reading from RAM takes on the order of 80 to 100 nanoseconds, while a core can execute hundreds of instructions in that time. So each core has small, fast caches close to it: a first-level cache of a few tens of kilobytes that answers in about a nanosecond, a larger second-level cache, and usually a third-level cache shared by many cores.
Caches do not store individual bytes or variables. They store cache lines, fixed-size blocks of memory, and on most current x86 and ARM server CPUs a line is 64 bytes. When our code reads one long, the CPU loads the whole 64-byte line that contains it. That is great for sequential access, because the next seven longs come along for free. It is also the root of false sharing.
| Where the data is | Typical size | Rough latency |
|---|---|---|
| L1 cache (per core) | 32 to 64 KB | about 1 ns |
| L2 cache (per core) | 1 to 2 MB | about 3 to 5 ns |
| L3 cache (shared) | tens of MB | about 10 to 20 ns |
| Main memory | GBs | about 80 to 100 ns |
2How Cores Keep Their Caches Consistent
If two cores each have a copy of the same line, and one core writes to it, the other core's copy is out of date. The CPU solves this with a cache coherence protocol, commonly a variant of MESI, named after the states a line can be in: Modified, Exclusive, Shared and Invalid.
The rule that matters for us is simple. Before a core can write to a line, it must own the line exclusively. So it sends a message that invalidates every other core's copy. If another core later wants to read or write anything in that line, it has to fetch the line again, often directly from the first core's cache. This transfer between cores costs tens of nanoseconds, much more than a write to a line that the core already owns.
3Why Unrelated Variables Interfere
Now picture two counters, a and b, stored next to each other in memory. Thread one on core 1 increments a, and thread two on core 2 increments b. They never touch each other's variable. But a and b are in the same 64-byte line, and the coherence protocol only knows about lines.
So each time core 1 writes a, it takes exclusive ownership of the line and invalidates core 2's copy. Then core 2 wants to write b, finds its copy invalid, takes the line back and invalidates core 1's copy. The line keeps bouncing, sometimes called cache line ping-pong, and every write that should take a nanosecond now waits for a transfer between cores. This is false sharing: the data is not shared, but the cache line is.
6 messages in order. Play them, or step through them one at a time.
Neither thread reads the other's variable, yet each write waits for the line to move.
4Where False Sharing Appears in Java
In Java we rarely control memory layout directly, but some layouts make false sharing likely. The classic case is a set of counters or flags declared next to each other in one object and updated by different threads, such as per-thread statistics, sequence numbers in a producer and a consumer, or head and tail indexes of a queue. Another common case is an array where thread i writes element i: a long[] of eight per-thread counters fits entirely in one 64-byte line.
Notice that the fix wastes memory on purpose. We trade a little space for each hot variable having a line of its own. The JDK itself does this inside classes such as LongAdder and some concurrent queues, using an internal annotation called @Contended that adds padding around fields. Padding of 128 rather than 64 bytes is common because some CPUs fetch pairs of adjacent lines together.
5When to Care
False sharing only matters when different threads write very frequently to nearby data, think millions of writes per second. A field updated once per request is not a problem. That is why it shows up in counters, queues, schedulers and low-latency systems, and almost never in ordinary business code. When we suspect it, we confirm it with a benchmark that compares the padded and unpadded layout, and on Linux with perf c2c, which reports cache lines that are contended between cores.
6Follow-up questions
Is false sharing a problem for reads?
No. Many cores can hold the same line in the shared state and read it as often as they like. The cost comes from writes, because a write requires exclusive ownership and invalidates the other copies.
Does volatile cause false sharing?
No, the layout does. volatile makes each write visible and ordered, which adds its own cost, and it is often used on exactly the hot fields that are prone to false sharing. But plain fields in the same line suffer from false sharing too.
Is the cache line always 64 bytes?
It is the most common size on x86 and many ARM server chips, but some CPUs, including Apple's M-series, use 128-byte lines, and some Intel CPUs prefetch adjacent pairs of lines. Code that pads should not assume exactly 64 bytes, which is why 128 bytes is a common padding choice.
7Conclusion
In this answer, we saw that CPUs cache memory in fixed blocks called cache lines, commonly 64 bytes, and that a coherence protocol forces a core to own a line exclusively before writing to it. When threads on different cores write to different variables in the same line, the line keeps bouncing between them, which is false sharing. It typically appears with counters, flags and indexes stored next to each other, and we fix it by giving each hot variable its own line after confirming the problem with a benchmark.
Open with
A cache line is the unit in which the CPU moves memory between RAM and its caches. On most current server CPUs it is 64 bytes, so when I read one long, the core actually loads the 64 bytes around it.
Then, if they want more
Because each core has its own caches, the hardware runs a coherence protocol, usually a MESI variant, to keep copies consistent, and the key rule is that a core must own a line exclusively before it writes to it, which invalidates every other core's copy.
False sharing is what happens when two threads on different cores write to different variables that live in the same cache line.
They share no data, but every write by one thread invalidates the line in the other core, so the line keeps bouncing between cores and each write that should take a nanosecond waits tens of nanoseconds for a transfer.
It typically shows up with hot counters, flags or sequence numbers declared next to each other in a class, or with an array where each thread writes its own slot. The fix is to pad so each hot variable has its own line, which is what the JDK does with @Contended in LongAdder.
I would only worry about it for very frequent writes, and I would confirm it with a benchmark or perf c2c.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is a CPU cache line, and what is false sharing?": https://scrivio-interview-library.netlify.app/java/java-performance/464aca41d7/