Java memory allocation principle (1)

Source: Internet
Author: User

 

Java memory allocation and management is one of the core technologies of Java. We have previously introduced Java memory management and Memory leakage and Java garbage collection knowledge. Today we go deep into Java core again, this section describes Java's knowledge about memory allocation in detail. Java generally involves the following areas in memory allocation:

◆ Register: we cannot control it in the program

◆ Stack: stores basic types of data and object references, but the object itself is not stored in the stack, but stored in the heap.

◆ Heap: stores data generated with new

◆ Static domain: static members defined by static in objects

◆ Constant pool: stores constants.

◆ Non-RAM storage: hard disks and other permanent storage space. Some basic types of variable data defined by the stack defined in the function in Java memory allocation 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. After 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. Heap memory in Java memory allocation is used to store objects and arrays created by new. The memory allocated in the heap is managed by the Java Virtual Machine's automatic garbage collector. 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 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 outside the code block where the new statement is 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 constant pool refers to the data that is identified during the compilation and saved 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:

◆ Full qualified names of classes and interfaces;

◆ Field name and descriptor;

◆ 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 the text string value, not the symbol reference. 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 the Method Area rather than in the heap. The heap and stack Java heap are a runtime data zone and class (objects are allocated space from them. 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 at the same time: 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 and point a to 3. Then process int B = 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.

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.