本地方法栈(Native Method Stacks)与虚拟机栈所发挥的作用是非常相似的,其区别不过是虚拟机栈为虚拟机执行Java方法(也就是字节码)服务,而本地方法栈则是为虚拟机使用到的Native方法服务。虚拟机规范中对本地方法栈中的方法使用的语言、使用方式与数据结构并没有强制规定,因此具体的虚拟机可以自由实现它。甚至有的虚拟机(譬如Sun HotSpot虚拟机)直接就把本地方法栈和虚拟机栈合二为一。与虚拟机栈一样,本地方法栈区域也会抛出StackOverflowError和OutOfMemoryError异常。

以上描述截取自:

《深入理解Java虚拟机:JVM高级特性与最佳实践》 作者: 周志明

----------------------------------------------------------------------------------------------------------------------------------

In addition to all the runtime data areas defined by the Java Virtual Machine

specification and described above, a running Java application may use other data

areas created by or for native methods. When a thread invokes a native method,

it enters a new world in which the structures and security restrictions of the

Java Virtual Machine no longer hamper its freedom. A native method can likely

access the runtime data areas of the virtual machine (it depends upon the native

method interface), but can also do anything else it wants. It may use registers

inside the native processor, allocate memory on any number of native heaps, or

use any kind of stack.

前面提到的所有运行时数据区都是Java虚拟机规范中明确定义的,除此之外,对于一个运行中的Java程序而言,他还可能会用到一些本地方法相关的数据区。当某个线程调用一个本地方法时,他就进入了一个全新的并且不再受虚拟机限制的世界 ,本地方法可以通过本地方法接口 来访问虚拟机得运行时数据区,但不止于此,他还可以做任何他想做的事情。比如,他甚至可以直接使用本地处理器中的寄存器,或者直接从本地内存的堆中分配任意数量的内存等等。总之,他和虚拟机拥有同样的权限(或者说能力)。

Native methods are inherently implementation dependent. Implementation

designers are free to decide what mechanisms they will use to enable a Java

application running on their implementation to invoke native methods.

本地方法本质上是依赖于实现的,虚拟机实现的设计者可以自由地决定使用怎样的机制来让Java程序调用本地方法。

Any native method interface will use some kind of native method stack. When a

thread invokes a Java method, the virtual machine creates a new frame and pushes

it onto the Java stack. When a thread invokes a native method, however, that

thread leaves the Java stack behind. Instead of pushing a new frame onto the

threadís Java stack, the Java Virtual Machine will simply dynamically link to

and directly invoke the native method. One way to think of it is that the Java

Virtual Machine is dynamically extending itself with native code. It is as if

the Java Virtual Machine implementation is just calling another (dynamically

linked) method within itself, at the behest of the running Java program.

任何本地方法接口都会使用某种本地方法栈。当线程调用Java方法时,虚拟机会创建一个新的栈帧并压入java栈。然而当他调用的是本地方法时,虚拟机会保持Java栈不变 ,不再在线程的java栈中压入新的帧,虚拟机只是简单地动态连接并直接调用指定的本地方法。可以把这看做是虚拟机利用本地方法来动态扩展自己 。就如同Java虚拟机的实现在按照其中运行的Java程序的吩咐,调用属于虚拟机内部的另一个(动态连接的)方法。

If an implementationís native method interface uses a C-linkage model, then

the native method stacks are C stacks. When a C program invokes a C function,

the stack operates in a certain way. The arguments to the function are pushed

onto the stack in a certain order. The return value is passed back to the

invoking function in a certain way. This would be the behavior of the of native

method stacks in that implementation.

如果某个虚拟机实现的本地方法接口是使用C连接模型的话,那个他的本地方法栈就是C栈。我们知道,当C程序调用一个C函数时,其栈操作都是确定的。传递 给该函数的参数已某个确定的顺序压入栈,他的返回值也以确定的方式传回调用者。同样,这就是改虚拟机实现中本地方法栈的行为。

A native method interface will likely (once again, it is up to the designers

to decide) be able to call back into the Java Virtual Machine and invoke a Java

method. In this case, the thread leaves the native method stack and enters

another Java stack.

很可能本地方法接口需要回调Java虚拟机中的Java方法(这也是由设计者决定的),在这种情形下,该线程会保存本地方法栈的状态并进入到另一个Java栈。

Figure 5-13 shows a graphical depiction of a thread that invokes a native

method that calls back into the virtual machine to invoke another Java method.

This figure shows the full picture of what a thread can expect inside the Java

Virtual Machine. A thread may spend its entire lifetime executing Java methods,

working with frames on its Java stack. Or, it may jump back and forth between

the Java stack and native method stacks.

图5-13描绘了这种情况,就是当一个线程调用一个本地方法时,本地方法又回调虚拟机中的另一个Java方法。这幅图展示了java虚拟机内部线程运行的全景 图。一个线程可能在整个生命周期中都执行Java方法,操作他的Java栈;或者他可能毫无障碍地在Java栈和本地方法栈之间跳转。

As depicted in Figure 5-13, a thread first invoked two Java methods, the

second of which invoked a native method. This act caused the virtual machine to

use a native method stack. In this figure, the native method stack is shown as a

finite amount of contiguous memory space. Assume it is a C stack. The stack area

used by each C-linkage function is shown in gray and bounded by a dashed line.

The first C-linkage function, which was invoked as a native method, invoked

another C-linkage function. The second C-linkage function invoked a Java method

through the native method interface. This Java method invoked another Java

method, which is the current method shown in the figure.

上图所示,该线程首先调用了两个Java方法,而第二个Java方法又调用了一个本地方法,这样导致虚拟机使用了一个本地方法栈。图中的本地方法栈显示为 一个连续的内存空间。假设这是一个C语言栈,期间有两个C函数,他们都以包围在虚线中的灰色块表示。第一个C函数被第二个Java方法当做本地方法调用, 而这个C函数又调用了第二个C函数。之后第二个C函数被第二个Java方法当做本地方法调用,而这个C函数又调用了第二个C函数。之后第二个C函数又通过 本地方法接口回调了一个Java方法(第三个Java方法)。最终这个Java方法又调用了一个Java方法(他成为图中的当前方法)。

As with the other runtime memory areas, the memory they occupied by native

method stacks need not be of a fixed size. It can expand and contract as needed

by the running application. Implementations may allow users or programmers to

specify an initial size for the method area, as well as a maximum or minimum

size.

就像其他运行时内存区一样,本地方法栈占用的内存区也不必是固定大小的,他可以根据需要动态扩展或者收缩。某些是实现也允许用户或者程序员指定该内存区的初始大小以及最大,最小值。

大小: 25 KB

分享到:

2011-11-02 10:16

浏览 18560

评论

java 本地方法栈_JVM学习笔记-本地方法栈(Native Method Stacks)相关推荐

  1. Java开发面试高频考点学习笔记(每日更新)

    Java开发面试高频考点学习笔记(每日更新) 1.深拷贝和浅拷贝 2.接口和抽象类的区别 3.java的内存是怎么分配的 4.java中的泛型是什么?类型擦除是什么? 5.Java中的反射是什么 6. ...

  2. 杨晓峰-java核心技术36讲(学习笔记)- 第1讲 | 谈谈你对Java平台的理解?

    杨晓峰-java核心技术36讲(学习笔记) 接下来我会分享杨晓峰-java核心技术36讲的学习笔记,内容较多,补充了其中一些牛人评论,相对详细(仅供个人学习记录整理,希望大家支持正版:https:// ...

  3. Java虚拟机(JVM)学习笔记(不定时更新)

    Java虚拟机(JVM)学习笔记 不少组织都曾开发过Java虚拟机: SUN公司曾经使用过3个虚拟机,Classic.Exact VM.Hotspot.     其中Hotspot虚拟机沿用至今,并已 ...

  4. 《Java并发编程实践》学习笔记之一:基础知识

    <Java并发编程实践>学习笔记之一:基础知识 1.程序与进程 1.1 程序与进程的概念 (1)程序:一组有序的静态指令,是一种静态概念:  (2)进程:是一种活动,它是由一个动作序列组成 ...

  5. 拉勾网《32个Java面试必考点》学习笔记之十一------消息队列与数据库

    本文为拉勾网<32个Java面试必考点>学习笔记.只是对视频内容进行简单整理,详细内容还请自行观看视频<32个Java面试必考点>.若本文侵犯了相关所有者的权益,请联系:txz ...

  6. (尚硅谷java零基础教程)学习笔记day7/8-数组

    1.数组的概述 1.1 定义 数组(Array),是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理. 1.2 数组的相关概念 数组名 元素 数组的索引 ...

  7. Java程序猿的JavaScript学习笔记(12——jQuery-扩展选择器)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  8. Java程序猿的JavaScript学习笔记(汇总文件夹)

    最终完结了,历时半个月. 内容包含: JavaScript面向对象特性分析,JavaScript高手必经之路. jQuery源代码级解析. jQuery EasyUI源代码级解析. Java程序猿的J ...

  9. 拉勾网《32个Java面试必考点》学习笔记之二------操作系统与网络知识

    本文为拉勾网<32个Java面试必考点>学习笔记.只是对视频内容进行简单整理,详细内容还请自行观看视频<32个Java面试必考点>.若本文侵犯了相关所有者的权益,请联系:txz ...

最新文章

  1. int与string转换
  2. 判断控件是否出现了滚动条
  3. 运维自动化轻量级工具pssh
  4. java面试题11 牛客:如下语句通过算术运算和逻辑运算之后i和 j的结果是
  5. mysql 8.0 手动安装教程_mysql 8.0.13手动安装教程
  6. 【剑指offer】5.二叉树的镜像和打印
  7. mysql实现斐波那契,C#实现斐波那契数列的几种方法整理
  8. Symfony 2.0 认识Request, Response, Session, Cookie
  9. 不那么SQL的SQL代码(一)if not exists(...) insert
  10. 案例1-合并2个不同文件夹中的csv文件到另外一个目录,相同的文件名进行数据合并,不同的文件名直接移到新文件夹...
  11. 「程序猿 DD」星球活动第一期正式开启!
  12. 适合长期电脑办公人群的养生小技巧
  13. 云服务器 共享文件,云服务器 共享文件
  14. 拉肚子差评回复模板_女子吃外卖烧烤后拉肚子给差评,老板电话骂人后还说“欧耶”...
  15. vue设置页面title
  16. BLE蓝牙广播和扫描主要数据设置解析与总结
  17. Socket.io之Socket类
  18. Protel的下载地址和学习资料
  19. 有盟分享开发存在的一些问题
  20. python自动化:uiautomation、pyautogui操作会计记账系统(3):处理序时簿

热门文章

  1. Google服务器架构解析
  2. 毕设走过的路——如何研究(一)
  3. 屎上最全vue-pdf+Springboot与aspose-words整合,开箱即用
  4. java中3.14是什么符号_java中3.14f是什么意思 C++问题下列选项中属
  5. 贴牌系统(SaaS)与定制开发的区别是什么?
  6. make_shared理解
  7. 『iOS开发』 —— UITextView实现信纸效果
  8. ID3 决策树的原理、构造及可视化(附完整源代码)
  9. 谈谈半年的信安学习和一些学习感悟(附收藏资料分享)
  10. 基础几何体的造型要点:看这几条总结很到位~