Basic knowledge about memory prototype of Java Virtual Machine (JVM)

Source: Internet
Author: User

Basic knowledge about memory prototype of Java Virtual Machine (JVM)

The memory prototype of the Java Virtual Machine consists of the following parts:

1. Register: we cannot control it in the program

2. STACK: stores basic data and object references, but the object itself is not stored in the stack, but stored in the heap.

3. Heap: store data generated with new

4. static fields: static members defined by static in objects

5. Constant pool: store Constants

6. Non-RAM storage: Permanent storage space such as hard disk

Stack in Java Memory Allocation

The basic unit of a stack is frame (or stack frame). When a java thread runs, the java VM allocates a java stack to the thread. When executing a java method, this thread pushes a frame to the java stack, which is used to store parameters, local variables, operands, intermediate calculation results, and so on. When this method is executed, frames will pop up from the stack. When a variable is defined in a code block, Java allocates memory space for the variable in 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 for another use immediately. However, we need to note the following points during operations:

1. All data on the Java stack is private, and no other thread can use the stack data of this thread.

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

3. The jvm has only one heap and is shared by all threads. The jvm does not store basic types and object references, but only stores objects and arrays;

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 collector of the Java Virtual Machine.

In contrast to the stack, the stack 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, you can 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, the variable in the stack becomes the referenced variable of the array or object. The referenced variable is equivalent to an array or an object name. Later, you can use the referenced variable in the stack in the program to access the array or object in the heap. The referenced variable is equivalent to an array or object name.

The Java heap is a runtime data zone and class (the object allocates space from it. These objects are created using commands such as new, newarray, anewarray, and multianewarray. They do not need program code to be explicitly released. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told in advance because the heap dynamically allocates memory at runtime, the Java Garbage Collector automatically collects the unused data. However, the slow access speed is due to the need to dynamically allocate memory during runtime.

Note: The created object only contains the member variables and does not include the member methods. Because objects of the same class have their own member variables and are stored in their respective stacks, but they share the methods of this class, instead of copying the member methods once every time an object is created.

Supplement: the reason why Java accounts for memory usage

The referenced variable is a common variable. It is assigned to the stack during definition. The referenced variable is released after the program runs out of its scope. Arrays and objects are allocated in the heap. Even if the program runs beyond the code block where new statements are used to generate arrays or objects, the memory occupied by arrays and objects is not released, arrays and objects become junk only when no referenced variable points to it. They cannot be used, but still occupy the memory space, the garbage collector collects (releases) the garbage collector at an uncertain time ). This is also the reason why Java accounts for memory usage.

In fact, the pointer in Java refers to the variable in the stack pointing to the variable in the heap memory!

The advantage of stack is that the access speed is faster than that of stack, second only to register, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. The stack mainly stores some basic types of variable data (int, short, long, byte, float, double, boolean, char) and object handle (reference ).

A very important feature of stacks is that data in stacks can be shared. Suppose we define both:

Inta = 3;

Intb = 3;

The compiler first processes inta = 3; first, it creates a reference with the variable a in the stack, and then finds whether the value 3 exists in the stack. If not, store 3 and point a to 3. Next, process intb = 3. After the referenced variable of B is created, B is directed to 3 because there is already 3 in the stack. In this way, both a and B point to 3 at the same time.

At this time, if a is set to 4 again, the compiler will re-search whether there are 4 values in the stack. If not, it will store 4 and make a point to 4; if yes, direct a to this address. Therefore, changing the value of a does not affect the value of B.

Note that the sharing of data is different from the sharing of two objects pointing to one object at the same time, because the modification of a does not affect B, which is completed by the compiler, it facilitates space saving. A variable referenced by an object modifies the internal state of the object, which affects the variable referenced by another object.

Constant pool)

A constant pool refers to some data that is determined during the compilation period and stored in the compiled. class file. Besides containing the basic types (such as int and long) defined in the Code and the constant values (final) of the object type (such as String and array) it also contains some symbolic references that appear in the form of text, such:

1. Full qualified names of classes and interfaces;

2. Field name and descriptor;

3. Method and name and descriptor.

The virtual machine must maintain a constant pool for each mounted type. For a String constant, its value is in the constant pool. A constant pool is an ordered set of constants used for this type, including direct constants (string, integer, and floatingpoint constants) and symbolic references to other types, fields, and methods. The constant pool in JVM exists in the memory as a table. For the String type, there is a fixed-length CONSTANT_String_info table to store the text String value. Note: this table only stores text string values, but does not store symbol references.

Note: During program execution, the constant pool is stored in MethodArea rather than in the heap.

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