Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of JVM Program counter and Stack

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of JVM program counters and stacks. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The Java virtual machine divides the memory it manages into several different data regions during the execution of the Java program. The image above shows the area of memory that the Java program needs to run, in which the local memory is not managed by the virtual machine but directly by the operating system, where the green memory area is private to each Java thread and the white memory area is shared by all Java threads.

Program counter

Because the Java program needs to switch threads during multithread execution, when the thread switches, it needs to record the location of the bytecode executed by the current thread, and the program counter saves the instruction address where the current thread needs to execute the bytecode.

If the thread is executing a Java method, the program counter records the address of the executing virtual machine bytecode instruction. If it is a Native method, the program counter value is Undefined.

Java stack

The Java stack is the private memory space of the thread. The execution of each Java method produces a Java stack frame. When the method is called, the stack frame will enter the stack, and when the method returns, the stack frame will be out of the stack. The main things that are stored in the Java stack (stack frames to be exact) are:

Local variable table Operand stack frame data area local variable table

The local variable table is used to hold the parameters of the function as well as local variables, storing various basic data types known at compile time, object references (which may be a reference pointer to the starting address of the object), or returnAddress types (the address of a bytecode instruction).

Public void stackSlot (int a, int b, long c) {{long d = 2; System.out.println (d);} StackObj obj = new StackObj (); int e = 3;}

The local variable table has the concept of slot (index). The local variable table of the above code is as follows:

The variables of the local variable table are important garbage collection root nodes, as long as the objects referenced directly or indirectly in the local variable table will not be recycled. Let's analyze the local variable scale:

The first slot of the local variable scale of the common method (the position where index is 0) is this, indicating that it points to itself, but the first slot of the local variable scale of the static method is the first variable.

The slot in the local variable table can be reused. If you look closely at the above figure, you will find that the obj variable reuses the slot of the variable d.

Operand stack

The Operand stack is mainly used to save the intermediate results of the calculation process, and also serves as a temporary storage space for variables in the calculation process.

Frame data area

The pointer that accesses the constant pool in the frame data area, and the exception handling table. The exception handling table can jump to the appropriate location of the bytecode when the program encounters an exception. If the appropriate handling method cannot be found in the exception table, it will end the current function call and throw the same exception in the calling function and find the exception table of the calling function for handling.

This is the end of this article on "sample analysis of JVM program counters and stacks". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report