JVM -- java stack

Source: Internet
Author: User
Tags vars

Each time a new thread is started, the Java Virtual Machine assigns a java stack to it. The java stack saves the running status of the thread in frames. The Virtual Machine will only perform two operations on the java stack directly: the frame-based pressure stack or the outbound stack. When a thread calls a Java method, the VM will press a new stack in the java stack of the thread.

Stack frame:

Stack frames are composed of three parts: local variables, operand stacks, and stack data zones.

When a virtual machine calls a Java method, it obtains the local variables and the size of the operation data stack from the corresponding class information, and allocates the stack frame memory accordingly, and press it into the java stack.

1) local variables: The local variable area of the java stack frame is organized into an array counted from 0 in bytes.

2) operand Stack: Like a local variable, it is organized into an array of characters in length. Access through standard stack operations-pressure stacks and outbound stacks.

3) stack data zone: the java stack supports constant pool resolution, normal method return, and some information about the exception dispatching mechanism.

Example:

A Fibonacci sequenceCode, As follows:

 
Class Fibonacci {static void calcsequence () {long fibonum = 1; long a = 1; long B = 1; for (;) {fibonum = a + B; A = B; B = fibonum ;}}}

Script code:

0 lconst_1 // push long constant 1 1 lstore_0 // POP long into local vars 0 & 1: long a = 1; 2 lconst_1 // push long constant 1 3 lstore_2 // POP long into local vars 2 & 3: long B = 1; 4 lconst_1 // push long constant 1 5 lstore 4 // POP long into local vars 4 & 5: Long fibonum = 1; 7 lload_0 // push long from local vars 0 & 1 8 lload_2 // push long from local vars 2 & 3 9 Ladd // pop two longs, add them, push result10 lstore 4 // POP long into local vars 4 & 5: fibonum = A + B; 12 lload_2 // push long from local vars 2 & 313 lstore_0 // POP long into local vars 0 & 1: A = B; 14 lLoad 4 // push long from local vars 4 & 516 lstore_2 // POP long into local vars 2 & 3: B = fibonum; 17 goto 7 // jump back to offset 7: for (;;){}

Virtual Machine image:

1) initialization. Local variables: local variable. Operand Stack: operand stack.


2) allocate three variables: fibonum, A, and B. A local variable is a three-digit array.

3) lload_0: load the long type value from the local variable 0, and lload_2: load the long type value from the local variable 2.

4) Ladd: Execute the addition of int type.

5) istore 4: The Int type value pops up from the stack and stores it in a local variable at position 4.

References:

Deep Java Virtual Machine

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.