Java Memory Model With Threads

Suppose the two threads are created on the same object.

So the member variables of that class object are stored in heap whether it is primitive or object.

But there is catch here while storing the local variables(variables declared inside a method of that class). The primitives are stored in the individual thread stack. But if a local variable points to an object, then the reference variable is stored in thread stack while actual object is stored in the heap.

Modern Hardware Architecture:

As you can see above, there are three memory storage units seen.

1)CPU Registers: It is a memory storage inside the CPU.

2)CPU Cache Memory: A cache for each CPU.

3)RAM: The physical memory or you can say the Main Memory.

The CPU can read data from main memory to CPU cache to CPU register or

from directly from main memory to cpu register.

Important thing to note here is that the heap space and the thread stack can be stored in CPU register, CPU cache memory or main memory.

Thanks for reading!!!

What is JVM?

JVM which stands for Java Virtual Machine can be considered as the heart of java.

It converts Java bytecode into machines code or you can say machine language. JVM is a part of Java Run Environment (JRE).

The bytecode which is converted by the JVM is created by the java compiler. The compiler converts the .java code or you can say the source code to byte code.

JVM is not platform-independent, that’s why you have different JVM for different operating systems.

The primary advantage of Java JVM is code compatibility as it eases a programmer’s job to write code only once and run anywhere. Once the application is built it can be run on any device that has JVM. So the job of the programmer is to write only the java code. The compiler and then the JVM takes care of the best.

Src:wiki commons

As seen above, the execution engine executes the bytecode which is assigned to the runtime data areas through the classloader.