In-depth parsing of Java memory prototype

Source: Internet
Author: User

Reprinted from: http://www.csdn.net/article/2012-06-29/2806999

This article mainly explains the working principle of Java memory allocation stack, heap and constant pool in detail.

I. Memory prototype of Java Virtual Machine

Register: we cannot control the stack in the program: store basic data and object references, but the object itself is not stored in the stack, but stored in the heap: static fields for storing data generated by new: static member constant pools defined by static in objects: static member constant non-ram storage for storing constants: Permanent storage space such as hard disks.

2. Constant pool)

A constant pool is determined during the compilation period and saved in compiled. Some data in the 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. A constant pool is an ordered set of constants used for this type, including direct constants (string, integer, and floating point constants) and symbolic references to other types, fields, and methods. For a String constant, its value is in the constant pool. 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. Here, we should have a clear understanding of the storage location of string values in the constant pool. During program execution, the constant pool is stored in Method
Area instead of heap.

3. 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. All data on the java stack is private, and no other thread can use the stack data of the thread. Variable data of some basic types defined in functions and referenced variables of objects are allocated in the function stack memory. 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.

4. 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 an object name.

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 variables in the stack point to the variables in the heap memory. This is the pointer in Java!

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.

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:

Int A = 3; int B = 3; the compiler first processes int A = 3; first, it creates a reference with the variable A in the stack, check whether the value 3 exists in the stack. If no value is found, store 3, point A to 3, and process int B = 3; after creating the reference variable of B, because the value 3 already exists in the stack, B is directly directed to 3, so that 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.

 

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.