JVM學習筆記-本地方法棧(Native Method Stacks)

本地方法棧(Native Method Stacks)與虛擬機棧所發揮的做用是很是類似的,其區別不過是虛擬機棧爲虛擬機執行Java方法(也就是字節碼)服務,而本地方法棧則是爲虛擬機使用到的Native方法服務。虛擬機規範中,對本地方法棧中的方法使用的語言、方法使用的方式與數據結構並無強制規定,所以具體的虛擬機能夠自由實現它。甚至有的虛擬機(譬如Sun HotSpot虛擬機)直接就把本地方法棧和虛擬機棧合二爲一。與虛擬機棧同樣,本地方法棧區域也會拋出StackOverflowError和OutOfMemoryError異常。程序員

以上描述截取自: 數據結構

《深刻理解Java虛擬機:JVM高級特性與最佳實踐》 做者: 周志明 app

---------------------------------------------------------------------------------------------------------------dom

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.ide

前面提到的全部運行時數據區都是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.this

本地方法本質上是依賴於實現的,虛擬機實現的設計者能夠自由地決定使用怎樣的機制來讓Java程序調用本地方法spa

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.

就像其餘運行時內存區同樣,本地方法棧佔用的內存區也沒必要是固定大小的,他能夠根據須要動態擴展或者收縮。某些是實現也容許用戶或者程序員指定該內存區的初始大小以及最大,最小值。

相關文章
相關標籤/搜索