In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the methods and skills of virtual machine stack through interview questions". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "through the interview questions virtual machine stack methods and skills" bar!
As we all know, the stack only has two operations: in-stack and out-stack, so it is a fast and efficient way to allocate storage. For it, there is no garbage collection problem, but its size is dynamic or fixed, so it has stack overflow or memory overflow problems.
Interviewer: excuse me, you just said there would be stack overflow and memory overflow problems. Can you tell us why this happened?
Ah Q: yes, we know that the virtual machine stack is made up of stack frames, and each method call corresponds to a stack frame. We can use the-Xss parameter to set the stack size, assuming that we set the virtual machine stack size is very small, when we call too many methods, that is, too many stack frames, there will be StackOverflowError, that is, stack overflow problem.
If our stack frame is not fixed, set to dynamic expansion, then when we do not have enough memory, there is not enough memory to support stack expansion, then there will be an OOM exception, that is, memory overflow problem.
Interviewer: mm-hmm (nodding), indicating that the young man's mind is very clear. well, you just said that setting the stack frame too small will lead to the problem of stack frame overflow, so the big point we set can completely avoid stack overflow.
Ah Q: as soon as I hear it, I want to dig a hole for me. As we generally advocate the Doctrine of the mean, when we hear this absolute question, we must be smart: no, adjusting the size of the stack can only delay the time of stack overflow or reduce the risk of stack overflow.
For example? Right?
Suppose a method call to a business logic takes 5000 times, but a stack overflow error is thrown at this time. We can get more stack space by setting-Xss so that the call will not overflow until 7000 times. At this point, it makes sense to resize the stack because it allows the business to support it normally.
Then if there is a dead recursion, no matter how much you increase the stack size, it will overflow, so it doesn't make any sense.
Interviewer: OK, then take a look at this simple Mini Program. Can you give me a general idea of how it is executed in memory?
Public void test () {byte I = 15; int j = 8; int k = I + j;}
Let's take a picture so that we can understand it better.
Ah Q: compile the code first, and then look at its bytecode file. As shown on the left side of the figure above, the execution process is as follows:
First, the instruction address 0 to be executed is stored in the PC register. In this case, the data of the local variable table and Operand stack is empty.
When executing the first instruction bipush, put Operand 15 on the Operand stack, and then set the value of the PC register to the execution address of the next instruction, that is, 2
When executing an operation instruction with an instruction address of 2, the data in the Operand stack is taken out and stored in position 1 of the local variable table. Because this method is an example method, the value of this is stored in bit 0, and the value in the PC register becomes 3.
In the same steps 2 and 3, 8 is put into the Operand stack, then taken out and stored in the local variable table, and the value in the PC register is also from 3-> 5-> 6.
When the address instructions are 6, 7, 8, the data with index positions 1 and 2 in the local variable table is reloaded into the Operand stack and the iadd addition operation is performed, the resulting value is stored in the Operand stack, and the value in the PC register is also from 6-> 7-> 8-> 9.
The operation instruction istore_3 is executed, the data in the Operand stack is taken out and saved to the position of index 3 in the local variable table, the return instruction is executed, and the method ends.
Interviewer: inner OS: this kid seems to be okay. That's not bad, can you say whether the local variables defined in the method are thread-safe?
A Q: let me give you a few more examples.
Public class LocalParaSafeProblem {/ * Thread-safe * although StringBuilder itself is not thread-safe, * the S1 variable only exists in the local variable table of this stack frame. * because the stack frame is an independent copy of each thread, * so S1 here is thread-safe * / public static void method01 () {/ / created within the thread. Belongs to the local variable StringBuilder S1 = new StringBuilder () S1.append ("a"); s1.append ("b");} / * * Thread unsafe * because StringBuilder is passed in as a parameter and * other external threads can also access it, so thread is unsafe * / public static void method02 (StringBuilder stringBuilder) {stringBuilder.append ("a"); stringBuilder.append ("b") } / * Thread unsafe * StringBuilder is operated by multiple threads at the same time * / public static void method03 () {StringBuilder stringBuilder = new StringBuilder (); new Thread (()-> {stringBuilder.append ("a"); stringBuilder.append ("b");}, "T1"). Start (); method02 (stringBuilder) } / * Thread is not safe * because the method returned StringBuilder at this time * other threads outside can directly modify the reference StringBuilder, so it is not safe * / public static StringBuilder method04 () {StringBuilder stringBuilder = new StringBuilder (); stringBuilder.append ("a"); stringBuilder.append ("b"); return stringBuilder } / * StringBuilder is thread safe * at this time the stringBuilder value exists in the local variable table of the current stack frame, * other threads cannot access the reference, * after the method execution is completed, the stringBuilder in the local variable table is destroyed * the returned stringBuilder.toString () thread is not safe * the final return value returns the toString Other threads can operate while String itself is not thread-safe. * / public static String method05 () {StringBuilder stringBuilder = new StringBuilder (); stringBuilder.append ("a"); stringBuilder.append ("b"); return stringBuilder.toString ();}} so far, I believe you have a deeper understanding of "the methods and skills of passing the virtual machine stack of interview questions". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.