DEV Community

ChelseaLiu0822
ChelseaLiu0822

Posted on

Stack and stack frame

Stack memory is for threads. After each thread is started, the virtual machine will allocate a piece of stack memory for it.
Each stack consists of multiple stack frames, corresponding to the memory occupied by each method call.
Each thread can only have one active stack frame, corresponding to the method currently being executed.
Thread Context Switch
Reasons that cause the CPU to no longer execute the current thread and instead execute the code of another thread:
The thread's CPU time is exhausted
Garbage collection
There are higher priority threads that need to run
The thread itself calls methods such as sleep, yield, wait, join, park, synchronized, and lock.
When a Context Switch occurs, the operating system is required to save the state of the current thread and restore the state of another thread.
Status, the corresponding concept in Java is Program Counter (Program Counter Register), its function is
Remember the execution address of the next jvm instruction, which is private to the thread.
The status includes the program counter, information about each stack frame in the virtual machine stack, local variables, operand stack, and return address.
wait
Frequent occurrence of Context Switch will affect performance

Top comments (0)