The program counter 01-JVM memory model: the program counter

01-JVM memory model: the program counter

 

A, JVM Model Overview

  java virtual machine (JVM) during the java program is running, it will divide these areas of memory management and some with JVM startup created data into a number of different areas, some users start threads with and ending the establishment and destruction. Memory model follows a basic JVM runtime:

  The figure shows that the "JAVA SE7" the JVM virtual machine specification. Note that the virtual machine specification is not static, Oracle when a new version of JAVA, JVM might do some optimization and improvement, for example in JDK8 versions, the method area is removed and replaced by metaspace (metadata space).

  In this and the following sections, will JDK7 standard as an example of the runtime data areas to explain the JVM.

Second, the program counter (Program Counter Register)

2.1) What is the program counter

  The program counter is a row number designator recorded bytecode executed by the current thread.
  After the code is compiled JAVA byte code without the JIT elapsed (time compiler) before compiling, its implementation is interpreted by "bytecode interpreter." A simple operating principle is loaded into memory to read a byte code interpreter, in order to read bytecode instructions. After reading an instruction, the instruction "translated" into a fixed operation and branches, loops, jumps and other processes based on these operations.
  From the above description may be generated if the program counter is superfluous questions. Continuing along sequential instruction execution because, even if such a flow is a branch jump, jump to the designated instruction is to continue in order to ensure complete execution of the sequence program. Assuming that the program is always only one thread, this question without any problems, that does not require the program counter. But in fact the program is a collaboration by multiple threads of execution.
  First we have to figure out the JVM's multi-threaded implementation. Multithreading is the JVM by the CPU round-robin (i.e., alternately switching the thread and allocated processor execution time) algorithm is implemented. In other words, because a thread might run out of time slice is suspended in the implementation process, while another thread time slice to get started. When the suspended thread to reacquire the time slice when it is suspended in order to continue from where it is necessary to know the location to which it was last executed in the JVM, to record a thread through the program counter bytecode execution position. Therefore, the program counter is characteristic with thread isolated, that is, each thread has its own independent work counter.

2.2) the characteristics of the program counter

  1. thread isolation, each thread has its own independent work counter.
  2. executed java method, there is a program counter value, and the recording is the address byte code instructions being executed (refer to the description of a measure).
  3. native native method execution, the program counter value is null (Undefined). Because native JNI method is invoked through direct local java C / C ++ library can be approximately considered equivalent to native methods C / C ++ interface exposed to the java, java by calling this interface to invoke the C / C ++ method. Since the method by C / C ++ java instead be implemented. So it can not produce the corresponding byte code, and memory allocation when the C / C ++ execution is determined by their own language, and not by the JVM.
  4. The small memory for the program counter, JVM memory during calculation, is negligible.
  The program counter, not only a predetermined region in any OutOfMemoryError java virtual machine specification.

A, JVM Model Overview

  java virtual machine (JVM) during the java program is running, it will divide these areas of memory management and some with JVM startup created data into a number of different areas, some users start threads with and ending the establishment and destruction. Memory model follows a basic JVM runtime:

  上图展示的是“JAVA SE7”的JVM虚拟机规范。注意,虚拟机规范并不是一成不变的,Oracle在发布新的JAVA版本时,可能会对JVM做一定的优化和改进,例如在JDK8的版本中,方法区被移除,取而代之的是metaspace(元数据空间)。

  在本章及下面的章节中,将以JDK7的标准作为例子,对JVM的运行时数据区进行讲解。

二、程序计数器(Program Counter Register)

2.1)什么是程序计数器

  程序计数器是一个记录着当前线程所执行的字节码的行号指示器。
  JAVA代码编译后的字节码在未经过JIT(实时编译器)编译前,其执行方式是通过“字节码解释器”进行解释执行。简单的工作原理为解释器读取装载入内存的字节码,按照顺序读取字节码指令。读取一个指令后,将该指令“翻译”成固定的操作,并根据这些操作进行分支、循环、跳转等流程。
  从上面的描述中,可能会产生程序计数器是否是多余的疑问。因为沿着指令的顺序执行下去,即使是分支跳转这样的流程,跳转到指定的指令处按顺序继续执行是完全能够保证程序的执行顺序的。假设程序永远只有一个线程,这个疑问没有任何问题,也就是说并不需要程序计数器。但实际上程序是通过多个线程协同合作执行的。
  首先我们要搞清楚JVM的多线程实现方式。JVM的多线程是通过CPU时间片轮转(即线程轮流切换并分配处理器执行时间)算法来实现的。也就是说,某个线程在执行过程中可能会因为时间片耗尽而被挂起,而另一个线程获取到时间片开始执行。当被挂起的线程重新获取到时间片的时候,它要想从被挂起的地方继续执行,就必须知道它上次执行到哪个位置,在JVM中,通过程序计数器来记录某个线程的字节码执行位置。因此,程序计数器是具备线程隔离的特性,也就是说,每个线程工作时都有属于自己的独立计数器。

2.2)程序计数器的特点

  1.线程隔离性,每个线程工作时都有属于自己的独立计数器。
  2.执行java方法时,程序计数器是有值的,且记录的是正在执行的字节码指令的地址(参考上一小节的描述)。
  3.执行native本地方法时,程序计数器的值为空(Undefined)。因为native方法是java通过JNI直接调用本地C/C++库,可以近似的认为native方法相当于C/C++暴露给java的一个接口,java通过调用这个接口从而调用到C/C++方法。由于该方法是通过C/C++而不是java进行实现。那么自然无法产生相应的字节码,并且C/C++执行时的内存分配是由自己语言决定的,而不是由JVM决定的。
  4.程序计数器占用内存很小,在进行JVM内存计算时,可以忽略不计。
  5.程序计数器,是唯一一个在java虚拟机规范中没有规定任何OutOfMemoryError的区域。

Guess you like

Origin www.cnblogs.com/simonfblog/p/12003507.html