Java knowledge learning - virtual machine stack and local method stack

The virtual machine stack, as the name suggests, is the same as the data structure we are familiar with - the stack. The virtual machine stack is the space required for threads to run, and each stack is composed of multiple stack frames. What is a stack frame? A stack frame corresponds to a method call, that is, the memory required for each method to run is the stack frame, and method parameters, local variables, and return addresses all take up space when the method runs. How are stacks and stack frames related? For example, when a piece of code calls the first method when it is executed, it will allocate a space for the first method and push it into the stack. When the first method is executed, the stack frame corresponding to this method will be removed from the stack. Remove, that is, release the corresponding memory, which is the relationship between the stack and the stack frame. There can be multiple stack frames in a stack.

Each thread can only have one active stack frame, corresponding to the currently executing method.

Does garbage collection manage stack memory? The answer is no, because the stack memory is nothing more than the stack frame memory generated by method calls again and again, and the stack frame memory will be popped from the stack after each method call, that is, it will be automatically recycled, so that is No garbage collection is required to manage stack memory. Garbage collection is mainly to reclaim useless objects in heap memory.

Is the larger the allocation of stack memory, the better? Stack memory can be specified by runtime virtual machine parameters (-Xss+size). The larger the stack memory division, the less the number of threads will be. The size of physical memory is fixed. For example, a thread uses stack memory. If a thread uses 1M memory, the physical memory is assumed to be 500M. In theory, I can have 500 threads run at the same time, but if each thread is set to use 2M of memory, theoretically only 250 threads run at the same time. Therefore, the larger the stack memory is not, the better it is. The larger the division is, it is only conducive to more method recursive calls and will not increase the efficiency of operation. Instead, it will affect the number of running threads.

Are local variables inside methods thread-safe? To see whether a variable is thread-safe, you only need to see whether it is shared or private by multiple threads. If the local variable in the method does not escape the scope of the method, it is thread-safe, and vice versa, there may be thread safety. If this local variable refers to the object and escapes the scope of the method, you need to consider thread safety issues.

There will be memory overflow in the virtual machine stack, and the memory overflow is caused by two aspects: too many stack frames (recursive calls have no end conditions) and too large stack frames.

Diagnosis of thread running - CPU usage is too high. Use the TOP command to locate which process is too high for the CPU, use the PS command to further confirm which thread causes the CPU to be too high, and finally use jstack + process ID (Jstack can find the problematic thread according to the thread ID, and further locate the problematic code line The number is in hexadecimal in Jstack, so you need to convert it when looking for it).

Diagnosis of thread running - the program runs for a long time with no results. You can also use the Jstack command to view specific thread problems and the number of specific problematic lines of code. Jstack will also display deadlock information at the end.

 

The local method stack, the local method stack actually needs to provide a memory space for the local method when the Java virtual machine calls the local method, which means that the method is not written by the Java code, the Java code has certain restrictions, sometimes It cannot directly deal with the bottom layer of our operating system, so it needs a local method written in C or C++ language to deal with the bottom operating system. Java code can indirectly call the local method to call some functions of the bottom layer. When the local method is running, it needs The memory used is our native method stack. For example, clone() and hashCode() in the Object class.

 

Guess you like

Origin blog.csdn.net/qq_35363507/article/details/104288779