当前位置:网站首页>JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter
JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter
2022-07-27 05:14:00 【_ Dean_】
JVM Part 1 : Memory and garbage collection – Runtime data area 4 - Program counter
1. Introduce
JVM Program count register in (Program Counter Register) in ,Register The name of comes from CPU The register of , register ** Store field information related to instructions .**CPU Only loading data into registers can run . here , It's not a physical register in a broad sense , Maybe translate it into PC Counter ( Or instruction counter ) It will be more appropriate ( Also known as program hook ), And it's not easy to cause some unnecessary misunderstandings .JVM Medium PC Registers are for physics PC An abstract simulation of registers .

It's a small piece of memory , I can almost ignore it . It's also JVM The fastest running storage area .
stay JVM Specification , Each thread has its own program counter , yes Thread private , The life cycle is consistent with the life cycle of the thread .
There is only one method in a thread at any time , It's the so-called current approach . The program counter stores what the current thread is executing Java Methodical JVM Instruction address ; perhaps , If it's execution native Method , The value is not specified (undefned).
- Local methods need to call C/C++ Library, so it is undefned
It's a program Indicator of control flow , Branch 、 loop 、 Jump 、 exception handling 、 Basic functions such as thread recovery rely on this counter .
Bytecode interpreterWhen you work, you know Select the next bytecode instruction to be executed by changing the value of this counter .It is the only one in Java Nothing is specified in the virtual machine specification outotMemoryError Area of situation .
- At the same time, it will not exist GC, Just store the address of the next instruction
2. effect
PC Registers are used to store the address to the next instruction , Also about to execute the instruction code . The execution engine reads the next instruction .

3. Code example
Write a simple code
public class PCRegisterTest {
public static void main(String[] args) {
int i = 10;
int j = 20;
int k = i + j;
String s = "abc";
System.out.println(i);
System.out.println(k);
}
}
Then compile the code into a bytecode file , Let's look at... Again , Find a line number to the left of bytecode , It's actually the instruction address , Used to point to where the current execution is .
0: bipush 10
2: istore_1
3: bipush 20
5: istore_2
6: iload_1
7: iload_2
8: iadd
9: istore_3
10: ldc #2 // String abc
12: astore 4
14: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
adopt PC register , We can know where the current program is going
The execution engine finds the operation command through the instruction address in the register , To manipulate the local variable table , Operand stack and direction CPU Send machine instructions

4. Two common problems of registers
4.1 Use PC What's the use of register to store bytecode instruction address ?/ Why use PC Register records the execution address of the current thread ?
because CPU You need to switch threads all the time , Now, after switching back , You need to know where to start and continue .
JVM The bytecode interpreter needs to be changed PC Register value Specify what bytecode instructions should be executed next .

4.2PC Why are registers set to be thread private ?
We all know that the so-called multithreading will only execute one of the methods in a specific period of time ,CPU I'll keep switching tasks , This inevitably leads to frequent interruptions or recoveries , How to make sure there is no difference ? In order to accurately record the current bytecode instruction address that each thread is executing , The best way, of course, is to assign one to each thread PC register , In this way, each thread can be independently calculated , So that there will be no mutual interference .
because CPU Time slice wheel limit , Many threads are executing concurrently , Any definite moment , A core in a processor or multi-core processor , Only one instruction in a thread will be executed .
This inevitably leads to frequent interruptions or recoveries , How to make sure there is no difference ? After each thread is created , Will produce their own program counter and stack frame , Program counters do not affect each other among threads .

5. CPU Time slice
CPU Time slice is CPU Time allocated to each program , Each thread is assigned a time period , It's called a time slice .
On the macro : We can open multiple applications at the same time , Each program runs parallel , Running at the same time .
But in On the micro : Since there is only one CPU, Only part of the program's requirements can be processed at a time , How to deal with fairness , One way is to introduce time slices , Each program is executed in turn .

边栏推荐
- JVM上篇:内存与垃圾回收篇九--运行时数据区-对象的实例化,内存布局与访问定位
- Detailed description of binary search tree
- Dialog introduction
- File processing (IO)
- 精选用户故事|洞态在聚水潭的误报率几乎为0,如何做到?
- Demo of throttling function -- regular expression matching
- Tcp server是如何一个端口处理多个客户端连接的(一对一还是一对多)
- 《Robust and Precise Vehicle Localization based on Multi-sensor Fusionin Diverse City Scenes》翻译
- Knapsack problem DP
- Interface and abstract class / method learning demo
猜你喜欢
![[error reporting] cannot read property 'parsecomponent' of undefined](/img/54/8d4225ec596d6b78348b181a3e636f.png)
[error reporting] cannot read property 'parsecomponent' of undefined

QT menu bar, toolbar and status bar

文件处理(IO)

The difference between strlen and sizeof

集成开发环境Pycharm的安装及模板设置

动态内存函数的介绍(malloc free calloc realloc)

Standard dialog qmessagebox

JVM上篇:内存与垃圾回收篇--运行时数据区四-程序计数器

Network protocol details: IP

How does the TCP server handle multiple client connections on one port (one-to-one or one to many)
随机推荐
对话框简介
The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay
SSM framework integration
Li Kou achieved the second largest result
Select user stories | the false positive rate of hole state in jushuitan is almost 0. How to do this?
Plato farm is expected to further expand its ecosystem through elephant swap
pyside2____1.安装和案列
SVN使用详解
Use ngrok for intranet penetration
Use of collection framework
探寻通用奥特能平台安全、智能、性能的奥秘!
Acticiti中startProcessInstanceByKey方法在variable表中的如何存储
Acceptance and neglect of events
Solution and principle analysis of feign call missing request header
Read write separation and master-slave synchronization
Introduction to Kali system ARP (network disconnection sniffing password packet capturing)
Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?
JVM上篇:内存与垃圾回收篇--运行时数据区四-程序计数器
深入 Qt5 信号槽新语法
树莓派rtmp推流本地摄像头图像