A summary of Java analysis-java virtual machine memory model (i) __java

Source: Internet
Author: User

A summary of Java Analysis-java virtual machine memory model


1 Program Calculator

Program counter is a small amount of memory space
Each thread has a separate program counter that records the next instruction to run, and the program calculator between the threads does not affect each other, it is a thread-private memory space 2 Java Virtual machine stacks

is the thread-private memory space that is created at the same time as the Java thread, which holds the local variables of the method, part of the result, and participates in the invocation and return of the method, which means that the lifecycle is the same as the thread, describing the memory model executed by the Java method

Each method executes with the creation of a stack frame, which stores information such as local variable tables, operand stacks, dynamic links, method exits, and so on. The execution of each method corresponds to the stack frame in the virtual machine stack in the stack, out of the stack process, LIFO (LIFO) stack. 2.1 The local variable table holds various basic data types, object reference types, and ReturnAddress types (address to a byte-code instruction: function return address) of the compile-time. The memory space required by the local variable table is determined at compile time, when a method is entered, The local variable control that the method needs to allocate in the stack frame is completely determined and cannot be changed dynamically.

A variable slot (Variable Slot) is the smallest unit of a local variable table, with no mandatory size of 32 bits, although 32 bits are sufficient to hold most types of data. A Slot can store 8 types of Boolean, Byte, char, short, int, float, reference, and ReturnAddress. Where reference represents a reference to an object instance, by which the index of the starting address of the object in the Java heap and the type information of the data type to which the data belongs is in the method area. ReturnAddress points to the address of a byte-code instruction. For 64-bit long and double variables, the virtual opportunity assigns them two contiguous Slot spaces.
the virtual machine uses the local variable table in the way of index positioning. As we know before, the local variables table holds the method parameters and the local variables. When the calling method is a non-static method, the Slot of the No. 0-bit index in the local variable table defaults to the reference used to pass the instance of the object to which the method belongs, that is, the object to which the "this" keyword points. When the method parameters are assigned, the local variables defined within the method are assigned sequentially. In
order to save the stack frame space, the Slot in the local variable table can be reused. After leaving the scope of some variables, the corresponding Slot of these variables can be given to other variables for use. This mechanism sometimes affects garbage collection behavior.

2.2 operand StacksLIFO LIFO, the maximum depth determined by the compile time. Stack frame just set up so that the operand stack is empty, the operand stack is used to store constants or variables that the JVM replicates from the local variables table, provides extraction, and results into the stack, as well as the parameters required by the calling method and the result of receiving the method return. The operand stack can hold values for any data type defined in a JVM. At any time, the operand stack is a fixed stack depth, the base type takes two depths except long and double, and the other occupies a depth 2.3 Dynamic Connection

Each stack frame contains a reference to the method that the stack frame belongs to in the Run-time pool, and holds the reference to support the dynamic connection during the method invocation. A large number of symbolic references exist in the constant pool of the class file, and the method invocation instruction in the bytecode is a parameter that refers to a symbol reference to a method in a constant pool. These symbolic references, some of which are converted into direct references (final, static, etc.) during class loading or first use, are called static parsing, and the other part is converted to a direct reference during each run, which is called the Dynamic Connection 2.4 method return address

When a method starts executing, there are only two ways to exit the current method:

• When execution encounters a return instruction, the return value is passed to the upper-level method caller, which is called the normal completion exit (normal method invocation completion) and, in general, the caller's PC counter can be used as the return address.
• When execution encounters an exception and the current method body is not processed, it causes the method to exit, at which point there is no return value, called an exception completion exit (abrupt method invocation completion), and the return address is determined by the exception handler table.

When the method returns, there are 3 possible actions:

• Restoration of local variable tables and operand stacks of upper-level methods
• The operand stack that pushes the return value into the caller caller stack frame
• Adjust the value of the PC counter to point to a directive following the method call instruction 3 Local method Stack

Similar to the Java Virtual machine stack, the Java Virtual machine stack is used to manage calls to Java functions, and the local method stack is used to manage calls to local methods.
The local method is a 4 Java heap implemented in C

Java heap can be said to be the most important part of Java Runtime Memory, and almost all objects and arrays are allocated space in the heap.
The Java heap can be divided into two parts of Cenozoic and old age,

Classification Description
Cenozoic Used to store newly created objects and young objects
Old age For storing objects with long surviving time

5 Java Method Area within a JVM instance, the type information is stored in a memory logical area called the method area. Type information is extracted by the class loader from the class file when the class is loaded.  Class (Static) variables are also stored in the method area. The method area is also called the static area, which, like the heap, is shared by all threads. The method area contains all the class and static variables the method area contains all the unique elements that are always in the program 6 in Java, there are six different places to store data 6.1 registers (register)

This is the fastest store because it is located in a different storage area--within the processor. But the number of registers is extremely limited, so the registers are allocated by the compiler according to the requirements. You can't control it directly, and you can't feel any sign of the register in the program. 6.2 stacks (stack) is located in Universal RAM, but it can be supported from the processor by its stack pointer. If the stack pointer moves downward, the new memory is allocated; If you move up, you release those memory. This is a fast and efficient method of allocating storage, second only to registers. When you create a program, the Java compiler must know the exact size and lifecycle of all the data stored in the stack, because it must generate the appropriate code to move the stack pointer up and down.  This constraint limits the flexibility of the program, so although some Java data is stored on the stack-especially object references-Java objects do not store them. 6.3 Heaps (heap)

A general-purpose memory pool (also in RAM) for storing Java objects that have been created. The advantage of a heap that differs from the stack is that the compiler does not need to know how many storage areas to allocate from the heap or how long the stored data will survive in the heap. Therefore, allocating storage in the heap has great flexibility. When you need to create an object, you just need to write a simple line of code that, when executed, will automatically store allocations in the heap. Of course, for this flexibility, you have to pay the appropriate code.  Storing allocations with a heap requires more time than storing storage on a stack. 6.4 static Storage (storage)

The "static" here means "in a fixed position." Data stored in a static store that always exists when the program is running. You can use the key words
Static to identify a specific element of an object is statically, but the Java object itself is never stored in static storage space. 6.5 constant Storage (constant storage)

Constant values are usually stored directly inside the program code, and it is safe to do so because they will never be changed. Sometimes, in an embedded system, the constants themselves are separated from other parts, so in this case, you can choose to place them in ROM 6.6 non RAM storage

If the data is completely alive outside of the program, it can be free of any program control and can exist when the program is not running.
Run class procedure: Method area Find Method-instantiated object in heap-call stack (point to instance in heap)

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.