Multithreading — memory management of threads

As we have discussed before in the posts, threads and processes are a bit different concepts. The difference between threads and processes in terms of memory management is that threads within one process run in a shared memory while processes run in a separate memory.

There are two types of memory — Stack memory & Heap Memory.

Stack memory is used for thread execution, it stores local variables, method calls and arguments within one thread. Stack memory is small and fast. Stack memory is always referenced in LIFO (Last-In-First-Out) order.

Heap memory stores objects that are created and the objects takes up heap memory until there is a reference to them from somewhere in the application. As soon as there is no reference to an object, the object is removed by the GC and heap memory is freed up. Heap memory is big and slow.

Each thread has its own cache and stack. Each stack stores local variables, method calls and method arguments inside its own stack. When there are threads running, thread #1 can not access local variables or methods of thread #2 and vice versa.

All the threads share the same memory and this is where we need to deal with the synchronization. When two threads try to access the shared object or variable, threads start race conditions that results in data corruption or data inconsistency. Dealing with race conditions and preventing data corruption is one the most challenging tasks of multithreading.

In this series of posts we have discussed about memory management of threads and processes.
Next, we will speak about synchronization, class level and method level synchronization, locks, etc.



The Path of Java Jedi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store