Internal Java Memory Model
Have you ever wondered how memory is allocated when you run a java program ?
If yes, then this blog will answer your question.The Java memory model specifies how the Java virtual machine works with the computer’s memory (RAM). The Java virtual machine is a model of a whole computer so this model naturally includes a memory model also called as the Java memory model.
Why do you need to understand Java Memory Model ?
A good understanding of Java memory model will help you write correctly behaving concurrent java programs. The Java memory model specifies how and when different threads can see values written to shared variables by other threads, and how to synchronize access to shared variables when necessary.
When you run a Java program a fixed memory space is allotted to JVM , this memory comprises of heap and stack section. The size of the memory can be increased but not beyond a certain limit and that makes sense as the memory is allocated on RAM.

The thread stack contains information about what methods the thread has called to reach the current point of execution(This is known as call stack). The thread stack changes as the code executes.Each thread has it’s own call stack where it stores the local variables used by the method and references to the objects if there are any. Point to be kept in mind is stack of a thread is visible to only itself and not any other thread. If two thread are executing the same code then they will have their own copies of local variable. All the primitive type local variables(boolean , byte , short , char , int , long , float , double) are stored inside the thread stack and thus are not visible to other threads , even if you are passing these variables to other threads, passing means you are just passing the copy of the variables.
The heap contains all objects created in your Java application at the runtime. When an object is created it is assigned a memory space in the heap regardless of the fact which thread created it. This object can be referenced by a local variable or it can be a member variable of an object.As we have already seen that a local variable is confined to a thread stack so in this case also reference to the object is stored in the thread stack whereas memory is assigned in the heap.Below diagram illustrated the memory structure which we have seen so far.

An object might as well have a method in which there can be local variables which might hold the reference to an object or a primitive data type , in this case also the local variables are stored in the thread stack.Objects on the heap can be accessed by all threads that have a reference to the object. When a thread has access to an object, it can also get access to that object’s member variables. If two threads call a method on the object of the same class at the same time but not referencing the same memory location , they will both have access to the object’s member variables, but each thread will have its own copy of the local variables . All the globally defined variables are also stored in the heap so that it is accessible by all the threads.
Below diagram illustrates what is a call stack , where the local variables are stored , how references to objects in the heap are stored in local variables and how a object can have other objects as member variables.
