The memory allocation mechanism of Java

Source: Internet
Author: User

Java programs run on the JVM (Java Virtual Machine,java VMs), the JVM can be understood as a bridge between the Java program and the operating system, the JVM implements the platform independence of Java, this shows the importance of the JVM. So when learning about the Java memory allocation principle, it is important to keep in mind that all this is done in the JVM, which is the basis and premise of the memory allocation principle.

A complete Java program will run with the following memory areas:

Register: JVM internal virtual register, access speed is very fast, the program is not controllable.

Stack: holds the value of a local variable, including: A. The value used to hold the base data type; b. Save an instance of the class, which is a reference (pointer) to the heap object . It can also be used to save the frame when the method is loaded.

heap: used to store dynamically generated data, such as new objects . Note the objects that are created contain only the member variables that belong to them, and do not include member methods. Because objects of the same class have their own member variables stored in their own heap, they share the methods of the class, and not every object is created to copy the member method one at a time.

constant Pool: The JVM maintains a constant pool for each loaded type, and the constant pool is an ordered set of constants used by this type. Includes direct constants (basic types, String) and symbolic references to other types, methods, and fields (1). The data in the pool is accessed through the same index as the array. Because a constant pool contains symbolic references to other types, methods, and fields for one type, Chang plays a central role in the dynamic linking of Java. A constant pool exists in the heap .

Code snippet: used to hold the source code read from the hard disk.

Data segment: a static member used to hold a static definition.

Represents a memory allocation diagram:

There are several points to note about between Java and memory:

1. A Java file, as long as there is a main entry method, we think this is a Java program, can be compiled and run separately.

2. Whether it is a variable of a normal type or a variable of a reference type (commonly known as an instance), it can be used as a local variable, and they can all appear in the stack. Only the normal type of variable in the stack directly save its corresponding value, and the reference type of the variable is a pointer to the heap area, through this pointer, you can find this instance in the heap corresponding to the object. As a result, a normal type variable occupies only one chunk of memory in the stack, whereas a reference type variable takes up a chunk of memory in the stack area and the heap area.

3. distinguish what an instance is and what an object is. Class a= New Class (), at which point A is called an instance, not a is an object. The instance is in the stack, the object is in the heap, and the operation instance is actually manipulating the object indirectly through the pointer of the instance. Multiple instances can point to the same object.

4. The data in the stack is not synchronized with data destruction in the heap. Once the method ends, the local variables in the stack are destroyed immediately, but the objects in the heap are not necessarily destroyed. Because there may be other variables that point to this object, it is destroyed only if there are no variables in the stack that point to the object in the heap, and it is not destroyed immediately, and can be destroyed when the garbage collection is scanned.

5. The above stacks, heaps, code snippets, data segments, and so on are all relative to the application. Each application corresponds to a unique instance of the JVM, and each JVM instance has its own area of memory that does not affect each other. And these memory areas are shared by all threads. The stacks and heaps mentioned here are concepts on the whole, and these stacks can be subdivided.

6 . The member variables of a class vary in different objects and have their own storage space (the member variable is in the object in the heap). The method of the class is shared by all objects of the class, only one set, the method is pressed into the stack when the object is used, and the method does not consume memory.

The above analysis involves only stacks and heaps, and there is a very important area of memory: constant Pools , where there are often inexplicable problems. What the hell is Chang? Above has been explained, there is no need to understand how deep, just remember that it maintains a loaded class of constants can be. A few points of attention in a constant pool.

1. The above mentioned basic types of packaging have implemented the constant pool technology, but they maintain the constant is only "128 to 127" in the scope of the constant, if the constant value beyond this range, will be created from the heap object, no longer from the constant pool fetch. For example, change the upper example to an integer i1 = 400; Integer I2 = 400, obviously over 127, cannot get constant from the constant pool, new integer object from the heap, then I1 and i2 are not equal.

2.The string type also implements the constant pool technique, but a little bit different. The string type is the first to detect that there is no corresponding string in the constant pool, and if so, to take it out, if not, to add the current one.

article references: A comprehensive analysis of Java memory allocation: http://blog.csdn.net/yangyuankp/article/details/7651251

The memory allocation mechanism of Java

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.