Java Review Basics--JVM memory structure (RPM)

Source: Internet
Author: User
Tags instance method visibility volatile

The main contents are as follows:

    • JVM startup process
    • JVM Basic Structure
    • Memory model
    • Compiling and interpreting the concept of running

First, the JVM startup process:

When the JVM starts, it is started by the Java Command/JAVAW command.

Second, the basic structure of the JVM:

JVM Basic structure diagram:

The description in "Understanding Java Virtual Machine (second edition)" is as follows:

Memory Allocations in Java:

Java programs need to allocate space in memory at run time. In order to improve the efficiency of computing, the data is divided into different space, because each region has a specific way of processing data and memory management.

This is divided into the following 5 memory spaces: (Very important)

    • Stacks: Storing local variables
    • Heap: Store all new things
    • Method area: Class information loaded by the virtual machine, constants, static constants, and so on.
    • Program counters (and system-related)
    • Local method Stack

1. Program Counter:

Each thread has a PC register

Created when thread is created

Point to the address of the next instruction

When you execute the local method, the value of the PC is undefined

2. Method Area:

Save the Loaded class information

Constant pool of type

field, method information

Method byte code

Usually associated with a permanent zone (Perm)

3. Heap Memory:

and program development is closely related

Application system objects are saved in the Java heap

All threads share the Java heap

For a generational GC, the heap is also generational

Major areas of GC management

Now the GC basically uses the Generational collection algorithm, if it is generational, then the heap is also generational. If the heap is generational, that heap of space should look something like this:

is the basic structure of the heap, which is explained in later articles.

4. Stack Memory:

    • Thread private, life cycle and thread same
    • Stacks are made up of a series of frames (so Java stacks are also called frame stacks)
    • Frame saves a method's local variables, operand stacks, Chang pointers
    • Each time the method call creates a frame and presses the stack

Explain:

The Java Virtual machine stack describes the memory model that is executed by the Java method: Each method is invoked to create a stack frame for storing local variable tables, operation stacks, dynamic links, method exits, and so on. Each method is called until the completion process corresponds to a stack frame in the virtual machine from the stack to the stack.

In the Java Virtual Machine specification, two exceptions are specified for this area:

(1) If the stack depth requested by the thread is too deep to exceed the depth allowed by the virtual machine, stackoverflowerror (such as infinite recursion) can occur. Because each stack frame occupies a certain amount of space, and XSS specifies the maximum space of the stack, exceeding this value will result in an error.

(2) The virtual machine stack can be dynamically extended, if it is extended to be unable to request enough memory space, the Oom will appear

4.1 Table of local variables for Java stacks: containing parameters and local variables

The local variable table holds the base data type, the object reference, and the ReturnAddress type (the address that points to a byte-code directive). The 64-bit length of long and double data takes up 2 local variable space (slots), and the remaining data types occupy only 1. The memory space required for the local variable table is allocated during compilation.

For example, I write the following code:

1 package test03; 2  3/** 4  * Created by Smyhvae on 2015/8/15.5  */6 public class Stackdemo {7      8     //static method 9 public     s tatic int runstatic (int i, long l, float F, Object O, byte b) {Ten         return 0;11     }12     //instance Method-Public     int Runinstance (char C, short S, Boolean b) {         0;16     }17 18}

In the above code, the static method has 6 parameters, and the instance method has 3 parameters. The corresponding local variable table is as follows:

In the table above, the static method and the local variable table corresponding to the instance method are basically similar. However, the following differences are: in the table of the instance method, the first position holds the reference to the current object.

4, 2 function calls of the Java stack make up the stack frame:

The method creates a stack frame each time it is called, for example, the following method:

public static int runstatic (int i,long l,float  f,object o, byte b) {       return runstatic (i,l,f,o,b);}

When it is called each time, a frame is created, and after the method call is finished, the frame is out of the stack. As shown in the following:

4.3 Java stack operand stack

Java does not have a register, all parameter passing is using the operand stack

For example, the following code:

    public static int Add (int a,int b) {        int c=0;        C=a+b;        return c;    }

The steps for pressing the stack are as follows:

0:ICONST_0//0 Press Stack

1:istore_2//eject int, stored in local variable 2

2:ILOAD_0//Put local variable 0 pressure stack

3:iload_1//local variable 1 pressure stack

4:iadd//Popup 2 variables, sum, result press stack

5:istore_2//Popup results, placed in local variable 2

6:iload_2//local variable 2 pressure stack

7:ireturn//Return

If the value of 100+98 is computed, then the change in the operand stack is as follows:

4.4 The stack on the Java stack:

Small objects (typically dozens of bytes), which can be allocated directly on the stack without escaping

Directly allocated on the stack, can be automatically recycled, reduce GC pressure

Large objects or escaped objects cannot be allocated on the stack

Stack, heap, method area interaction:

Three, the memory model:

Each thread has a working memory. Working memory and main storage are independent. The working memory holds a copy of the value of the variable in main storage.

When data is copied from main memory to the working store, there must be two actions: first, the read operation performed by the main memory, and the corresponding load operation performed by the working memory, and two actions when the data is copied from the working memory to the main memory: the first one, Storage (store) operations performed by working memory, and second, corresponding write operations performed by the main memory.

Each operation is atomic, that is, the execution period is not interrupted

For normal variables, the updated value in one thread cannot be immediately reflected in other variables. If you need to be visible immediately in other threads, you need to use the volatile keyword as the identity.

1. Visibility:

A thread modifies a variable, and other threads can immediately know

Ways to ensure visibility:

Volatile

Synchronized (before unlock, write variable value back to main memory)

Final (other threads will be visible once the initialization is complete)

2, the Order of:

Within this thread, the operation is orderly.

On-line observation, operations are unordered. (Command reflow or main memory sync delay)

3. Order rearrangement:

Command rearrangement: Destroys the order between threads:

Command rearrangement: A method of guaranteeing order:

Basic principles of command rearrangement:

Procedural Order Principle: the serialization of semantics within a thread

Volatile rule: The write of a volatile variable that occurs first in the Read

Lock rule: Unlocking (unlock) must occur before the subsequent locking (lock)

Transitivity: A precedes b,b before C then a must precede C

The start method of a thread precedes every action of it

All operations of the thread precede the end of the thread (Thread.Join ())

Thread Interrupt (interrupt ()) precedes the code of the interrupted thread

Object constructor execution ends before the Finalize () method

Iv. explain the concept of run and compile run:

Explanation Run:

Interpreting execution to run bytecode in an interpreted manner

Explanation of execution means: read a sentence of execution

Compile-run (JIT):

Compile bytecode into machine code

Execute machine code directly

Run-time compilation

Post-compilation performance with an order of magnitude improvement

The performance of the compilation run is better than the interpretation run.

Turn the arrogant guy: http://www.cnblogs.com/smyhvae/p/4748392.htm

Java Review Basics--JVM memory structure (RPM)

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.