Search code examples
javajvmspecificationsjava-memory-model

Is JVM working memory the same as CPU cache


The JVM spec says that each thread has its own working memory and lists several operations including use, assign, load, read, store, write, lock, unlock. Is working memory the same as the CPU cache, or is working memory the same as thread stack?

The spec also says that "As the thread executes a program, it operates on these working copies." So when JVM execute instructions to add two values on the operand stack, it pops the two operands from stack and add them. In this case, what and where are the working copies of these popped operands?


Solution

  • You are looking at the oldest specification that is still online at Oracle (at the bottom of the page). We’re talking about nothing less than 16 years having passed since the release of this document. But even within the outdated document, you’ve found a chapter that was outdated by the time of the release already.

    As the beginning of the chapter states.

    It has been adapted with minimal changes from Chapter 17 of the first edition of The Java™ Language Specification, by James Gosling, Bill Joy, and Guy Steele.

    Note that it says that it is based on the first edition of the JLS whereas, when you look at the overview page you’ll notice that the third edition of the JLS was the right one to describe the behavior of Java 6. It’s worth knowing the historical context, i.e. that for Java 5, the Java Memory Model has been completely rewritten, so we’re not talking about minor changes between the three editions of the Java Language Specification.

    Starting with the Java 7 version of the Java Virtual Machine Specification, there are no copies of the JLS sections anymore, so there’s no risk of getting out of sync again. The authoritative source of the Java Memory Model is the language specification’s chapter.

    It doesn’t use the terms “working memory” nor “master copy” at all, which is a good thing, as it is wrong to think about the JVM’s state as having some kind of absolute truth in a main memory.

    If you want to know more about the JLS or JVMS, you should start with the newest specifications, at the beginning of the overview page and only if you have a reason to assume that there might be a historical difference you have to know, consult an older specification.