Java memory model and local variable [closed]

Tags: ,



This question is related to java memory model.

I have a java method:

public class DataUtil{
  public void process(){
    int c=0;
    c=c+1;

    System.out.println(c);
  }
}

In the line “System.out.println(c)”, where does the println method takes the value of c variable and prints it on screen? CPU cache, or RAM

Answer

To be clear, the question and this answer are solely about the behavior of a program with one thread. Many of the things I say below may not apply to multi-threaded programs.

Where does the println method takes the value of c variable and prints it on screen? CPU cache, or RAM

The Java Memory Model (JLS 17.4) says nothing definite about cache and RAM. In general, it specifies visibility behavior without prescribing the way that a particular compiler implements that behavior. The JMM mandates that there is a happens before relation when a thread writes a variable and subsequently reads is. The happens before constrains the generated code to behave in a certain way. However it does not mandate any particular implementation approach for achieving those constrains.

In your example, the JMM doesn’t even place an implicit constraint on whether the value comes from cache or RAM. The c variable is only accessible to one thread. (It is a local variable!). So the compiler could (in theory) store the variable’s value anywhere1. The only constraint is that the most recent value is used when the variable is accessed. The compiler just needs to keep track of where the most recent variable is kept …

As a general rule, the JMM only has something interesting to say about variables and objects that are shared by different threads.


1 – In a register, in RAM, in cache memory, on a hardware stack … even written on a piece of paper shoved down the back of the sofa, if your hardware platform supports that.


If you are using “memory model” in a broader sense, then the plain answer is “we cannot say”.

  • In languages (not Java!) where the memory model is specified in terms of main memory and cache, the memory model would most likely not constrain this example.

  • If you are not talking about any specific memory model, but just asking if the value is fetched from cache or RAM, then we cannot say … because this is an implementation detail of the language implementation. For example, the JVM.

What we can say with a high level of certainty2 is that the implementation will fetch and print the most recent value of c from somewhere in this example.

In the case of Java, the JLS says that it must return the most recent value. (It’s in JLS 17.4 if you want to look.) The JLS leaves it up to the Java implementation to decide how to do that.

It is safe to assume that any JVM implementation will have come up with a reliable solution; i.e. that the most recent value of a variable will be used. But figuring out the details would be a big task … and (IMO) would not worth the effort. (You don’t need to understand the internals of a Volvo 264 automatic gearbox to drive a car.)


2 – We can be certain because there aren’t bazillions of bug reports of single threaded application not working due to problems reading and writing variables. Also, if there are any doubts, it is possible to examine the JIT compiler source code to understand what it does, or analyze the native code that it generates.



Source: stackoverflow