Home >Java >javaTutorial >Detailed explanation of Java virtual machine memory prototype examples

Detailed explanation of Java virtual machine memory prototype examples

零下一度
零下一度Original
2017-07-21 22:04:341310browse

Six parts of the Java virtual machine memory prototype:

1. Register: We cannot control it in the program

2. Stack: Stores basic types of data and object references, but The object itself is not stored on the stack, but is stored in the heap

 3. Heap: stores data generated using new

4. Static domain: stored in the object static defined with static Member

5. Constant pool: store constants

6. Non-RAM storage: hard disk and other permanent storage space

Detailed explanation of Java virtual machine memory prototype examples

Java memory allocation Stack

The basic unit of the stack is the frame (or stack frame): whenever a Java thread runs, the Java virtual machine allocates a Java stack to the thread. When the thread executes a certain Java method, it pushes a frame into the Java stack. This frame is used to store parameters, local variables, operands, intermediate operation results, etc. When this method completes execution, the frame will be popped from the stack. When a variable is defined in a block of code, Java allocates memory space for the variable on the stack. When the variable exits the scope, Java will automatically release the memory space allocated for the variable, and the memory space can be used immediately. used for other purposes. But when we operate, we need to pay attention to the following points:

1. All data on the Java stack is private, and other threads cannot access the stack data of this thread.

2. All objects are stored, and each object contains information about a corresponding class (the purpose of class is to obtain operation instructions);

3. JVM has only one heap area (heap), and is shared by all threads. Basic types and object references are not stored in the heap, only the object itself and the array itself are stored;

 栈的基本单位是帧(或栈帧):每当一个java线程运行的时候,java虚拟机会为该线程分配一个java栈。

Heap in Java memory allocation

The heap in the Java virtual machine is used to store objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collection mechanism of the Java virtual machine.

Simply speaking, compared with the stack, the heap is mainly used to store Java objects, and the stack is mainly used to store object references... After an array or object is generated in the heap, it can also be added to the stack Define a special variable in the stack so that the value of this variable in the stack is equal to the first address of the array or object in the heap memory. This variable in the stack becomes a reference variable of the array or object. A reference variable is equivalent to giving a name to an array or object. You can then use the reference variable in the stack to access the array or object in the heap in the program. A reference variable is equivalent to giving a name to an array or object.

Java's heap is a runtime data area from which class objects allocate space. These objects are created through instructions such as new, newarray, anewarray, and multianewarray, and they do not require program code to be explicitly released. Heap It is responsible for garbage collection. The advantage of the heap is that it can dynamically allocate memory size, and the lifetime does not need to be told to the compiler in advance, because it dynamically allocates memory at runtime, and Java's garbage collector will automatically collect these unused The data is reused. But the disadvantage is that due to the dynamic allocation of memory at runtime, the access speed is slow.

Note: The created objects only contain their own member variables and do not include member methods. Because objects of the same class have their own member variables and are stored in their own heaps, but they share the methods of the class, the member methods are not copied every time an object is created.

Supplement: Java comparison. Reasons for occupying memory

Reference variables are ordinary variables that are allocated on the stack when they are defined. The reference variables are released after the program runs outside their scope, while the arrays and objects themselves are allocated on the heap. When the program runs outside the code block where the statement using new to generate an array or object is located, the memory occupied by the array and object itself will not be released. Arrays and objects only become garbage when there are no reference variables pointing to them, and cannot be used again. Used, but still occupying memory space, it will be collected (released) by the garbage collector at an uncertain time. This is also the reason why Java takes up more memory. In fact, in Java. The pointer refers to the variable in the stack pointing to the variable in the heap memory!

The advantage of the stack is that the access speed is faster than the heap, second only to the register, but the disadvantage is that the stack data can be shared. The data size and lifetime must be determined and lack flexibility. The stack mainly stores some basic types of variable data (int, short, long, byte, float, double, boolean, char) and object handles (references)

A very important special feature of the stack is that the data stored in the stack can be shared. Suppose we also define:

inta=3;

intb=3;##. #

The compiler first processes inta=3; first it creates a reference to the variable a on the stack, and then checks whether there is a value of 3 on the stack. If not found, it stores 3 in, and then points a to 3. . Then process intb=3; after creating the reference variable of b, because there is already a value of 3 on the stack, b will be pointed directly to 3. In this way, there is a situation where a and b both point to 3 at the same time.

At this time, if a=4 is set again; then the compiler will re-search whether there is a 4 value in the stack. If not, it will store 4 in and make a point to 4; if it already exists, then Directly point a to this address. Therefore, changes in the value of a will not affect the value of b.

It should be noted that this kind of data sharing is different from the sharing in which the references of two objects point to one object at the same time, because in this case the modification of a will not affect b, it is determined by the compiler Complete, it helps save space. If an object reference variable modifies the internal state of the object, it will affect another object reference variable.

Constant pool (constantpool)

Detailed explanation of Java virtual machine memory prototype examples

# The constant pool refers to some resources that are determined during compilation and saved in the compiled .class file. data. In addition to the constant values ​​(final) that contain various basic types (such as int, long, etc.) and object types (such as String and arrays) defined in the code, it also contains some symbol references that appear in text form, such as:

 1. Fully qualified names of classes and interfaces;

 2. Names and descriptors of fields;

 3. Methods, names and descriptors.

The virtual machine must maintain a constant pool for each loaded type. For String constants, their values ​​are in the constant pool. The constant pool is an ordered set of constants used by this type, including direct constants (string, integer and floating point constants) and symbolic references to other types, fields and methods. The constant pool in the JVM exists in the form of a table in memory. For the String type, there is a fixed-length CONSTANT_String_info table used to store literal string values. Note: This table only stores literal string values, not symbols. Quote.

The above is the detailed content of Detailed explanation of Java virtual machine memory prototype examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn