Q21. What is thread stack and Heap space as per Java Memory model?

Ans: Java memory model divides reserved memory in two separate area known as thread stack and Heap Space.

Thread Stack: It is specific to each thread. If you have 100 threads running in your application. You will be having 100 thread stacks. Thread stack keeps information in “call stack”. Thread stack also keeps all local variables for each method. Local variable in thread stack will not be visible to another thread hence, thread safe.

Call Stack: Call stack keep the information about all the method it has executed till this point. For instance there are 5 method named as method1(), method2().. method5().. and you are currently on method3(). It will keep information on call stack as below. However, local variable are GCed as soon as thread completes the method execution.

method3()

method2() : local variable of method2 as well

method1() : local variable of method2 as well

 

Heap Space: It contains all objects created in your java application. It can be created by any thread. All objects will shared same memory space. Objects could be local variable or member variable all will be stored in heap space.

Q22. Static class variable stored in Heap space or in Thread stack?

Ans: Static class variables are stored on Heap space, with the class definition.

Q23. How java memory model mapped to Hardware memory on multiple CPU?

Ans: Let’s first understand Hardware memory architecture: There are mainly three sections of memory on any computer (Remember your college time computer architecture syllabus).

· RAM: Main Memory (It is shared across CPUs). It is the biggest memory area for JVM.

· CPU Cache: Each CPU has its own CPU cache. Accessing CPU cache is faster than main memory but slower than CPU registers.

· CPU Registers: Each CPU has its own Registers as part of CPU memory. Most of the calculations happen on CPU registers and it will be much faster.

Each CPU is capable of running single thread at any moment. So if we have multithreaded application (each thread will be running on separate CPU, assume we have 10 threads and 2 CPUs, then only two threads at a time can execute concurrently out of 10 threads.)

Q24. What is the Role of RAM, CPU cache and Registers in java process?

Ans: Whenever, any calculations/process needs to be executed by a thread. It will read value from main memory and load into CPU cache. And from CPU cache to registers.

Once calculation is completed resulting variable returned from registers to cache and flushed from cache to main memory.

From CPU Cache to main memory flushing happens only when CPU needs more memory to do some calculations or stored new variable

Q25. How Thread stack and Heap Space mapped to this Hardware memory model.

Ans: So at hardware level there is no difference between Thread Stack and Heap Space. It is JVM responsibility to keep them separate. Thread Stack will share have their variables stored in any of the Hardware memory area e.g. Main Memory, CPU Cache and Registers. Similarly Heap Space can also share any of the memory area.