In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what is the use of Java virtual machine stack jvm stack, I hope you will gain something after reading this article, let's discuss it together!
1. Define
Stack: the memory space needed by a thread to run. There are multiple stack frames in a stack. The stack has the characteristics of first-in-first-out, last-in-first-out.
Stack frame: the memory required for each method to run (local variables, Operand stacks, dynamic links, and method return values, etc.). Each time a method is called, the stack frame is pressed into the stack, and the stack frame is pushed out from the top of the stack after the method is executed
Active stack frame: refers to the stack frame at the top of the stack, the method being called. Each thread can only have one active stack frame, corresponding to the method that the thread is calling.
Now let's use code to demonstrate how the Java virtual machine pushes stack frames into and out of the stack.
Public class Main {public static void main (String [] args) {method1 ();} private static void method1 () {method2 (1,2);} private static int method2 (int a, int b) {int c = a + b; return c;}}
When we run the Main function, jvm first pushes the stack frame Main into the stack, and the stack structure is shown in the figure
The method1 method is called in the body of the Main function, and stack frame 1 is pushed into the stack.
When the method2 method is called in the body of the method1 method, jvm will push stack frame 2 into the stack structure. It should be noted that the stack frame is composed of information such as "local variable table, Operand stack, dynamic link and method return value". The method2 method has two parameters, a _ Band, and a local variable c and a method return value.
When the method2 method is executed, stack frame 2 pops up from the top of the stack
After the execution of the method1 method, pop up stack frame 1
In turn until the thread is destroyed.
Pay attention
Since each stack frame is popped up after the method call, the stack memory does not need to be garbage collected
Each stack is thread private, and each thread creates a virtual machine stack when it is created, and because the physical memory is fixed, the larger the stack memory partition, the fewer threads can be allocated.
two。 Thread safety of stack
Local variables are thread-safe
Now let's define the following methods:
Public static void method () {int a = 0; axioms;}
We define a local variable an in the method method and perform the await + operation on it. Now suppose we have two threads calling this method (stack frame) at the same time, and the Java virtual machine presses the stack frame into the stack memory of their respective threads, but because the local variable table is thread private, after both threads call this stack frame at the same time, the value of an is still 1, so the local variable is thread safe.
Method parameters and method return values are not thread safe
Because the parameters and return values of methods can be referenced by external methods, one method can change the parameters and return values of another method under one thread, so the parameters and return values of methods are not thread-safe.
3. Stack memory overflow
Because the stack memory space allocated by the virtual machine to each stack is fixed when the program is executed, there may be insufficient stack memory space in some cases, resulting in overflow. Generally, there are two cases that may lead to stack memory overflow.
Stack frame is too large (rarely occurs)
Too many stack frames (usually occurs when recursive. Recursive exit is not set correctly)
Now let's explain when too many stack frames will lead to stack memory overflow. Let's assume that the memory space of a stack is 1024kb. Now there are four stack frames, each of which is 300kb, and 300 * 4 = 1200kb, and 1200 > 1024. Obviously, now that the combined size of these four stack frames has exceeded the memory space of the stack, stack memory overflow will occur. That is, the error of java.lang.StackOverflowError will be reported.
After reading this article, I believe you have a certain understanding of "what is the use of Java virtual machine stack jvm stack". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.