Skip to main content

Jave Memory Models (JMM):

What is the Java Memory Model?

The Java memory model specifies how the Java virtual machine works with the computer's memory (RAM). The Java virtual machine is a whole computer model, so it naturally includes a memory model - AKA the Java memory model.


It is vital to understand the Java memory model if you want to design correctly behaving concurrent programs. The Java memory model specifies how and when different threads can see values written to shared variables by other threads and how to synchronise access to shared variables when necessary.


A memory model is best thought of as the architecture that determines how different parts of memory (i.e. stack and heap) interact. The Java Virtual Machine (JVM) divides the memory into two logical units:

  • Thread stack
  • Heap

Thread stack

The thread stack is the part of the memory that stores the data specific to a thread. Every thread has its own thread stack. The thread stack stores local variables (both primitive and reference variables) of each method, ​whether the method belongs to an object or not.


When two separate threads call the same method, each thread creates its copy of the local variables declared in the method. Therefore, one thread cannot directly access the variables of another thread.

Heap

There is only one shared heap for the whole Java application. It stores the objects created and their member variables (both primitives and references). Any thread that has a reference to an object on the heap can access its member variables. Moreover, a single object on the heap can be referenced by the local variables of different threads.

Popular posts from this blog

What happens if we don't override the run() method:

Consequences of not overriding the run() method: If we don’t override the run() method, the compiler will not flash any error, and it will execute the run() method of the Thread class that has been emptily implemented, So, there will be no output for this Thread. Below is an example of the method returning no output: public class Example extends Thread { public static void main(String[] args) { Thread thread = new Thread( new Example()); thread.start(); // will create a new thread and call thread class's run() method which has no implementation. } }

Executer vs ExecuterSevice: What's the Difference?

Executer and ExcuterService: Executor and ExecutorService are part of Java's Executor framework, which provides  thread pool  facilities to Java applications. Since creating and managing Threads are expensive, It's a better idea to create a thread pool that can execute in parallel rather than every time a new thread is requested, helping reduce the load. This improves the response time of the application. Differences between Executer and ExecuterService: One of the key differences between the Executor and ExecutorService interface is that the former is a parent interface while ExecutorService extends Executor.  A second significant difference between ExecutorService and Executor is that Executor defines an execute() method, which accepts the Runnable interface object. Simultaneously, a submit() method takes both Runnable and Callable object interfaces. Another difference between Executor and ExecutorService interface is that the execute() methods' return type is void, wh...

What is Deadlock, Livelock and Starvation?

Deadlock, Livelock and Starvation: Deadlock: Deadlock   is where a set of processes are blocked because each process is holding a resource and waiting for another resource that is doing the same thing.  An example of this is when two trains are coming toward each other on the same track. Neither of the trains can move once they are in front of each other (assuming there are no connecting tracks).  Livelock: Livelock  occurs when two or more processes continue to repeat in response to changes in other processes without achieving any productive outcome. These processes do not get placed in the waiting state but continue to run without any productive outcome. This differs from a deadlock because, in a deadlock, all processes are put in the waiting state. Starvation: Starvation is a problem that is closely related to both Livelock and Deadlock. This can occur as a policy is used to determine who gets access to the resource when it can lead to some processes never getting...