Deep Dive into Java Virtual Machine: Stack and heap in JVM

Source: Internet
Author: User

In JVM, memory is divided into two parts: Stack and heap. Here, we understand the stack and heap from the perspective of JVM memory management principles, through these principles, we can identify static methods and static attributes in Java.

Average,JVMMemory is divided into two parts: Stack and heap.

Stack(Stack) is the memory instruction area of JVM. Stack management is very simple. It pushes data or commands of a certain length of bytes, And the stack pointer presses the corresponding byte displacement of the stack. The pop refers to data or commands of a certain length of bytes, And the stack pointer plays the stack. Stack is fast and easy to manage, and the length of data or instruction bytes for each operation is known. Therefore, the basic data types of Java, Java instruction code, and constants are stored in the stack.

Heap(HEAP) is the JVM memory data zone. The management of heap is very complicated. Heap allocates an indefinite amount of memory space each time and is used to save the instance of the object. Allocate a certain amount of memory in heap to save the object instance. In fact, it only saves the attribute values, attribute types, and object type tags, the method of not saving the object (the method is to run the command and save it in the stack). The heap allocates a certain amount of memory to save the object instance and the serialization of the object is similar. After the object instance is allocated in heap, a 4-byte heap memory address needs to be saved in the stack to locate the location of the object instance in heap, so as to locate the object instance easily.

Because the memory management of the stack is sequentially allocated and fixed, there is no memory recycling problem; While heap is randomly allocated memory with an indefinite length, there is a problem of memory allocation and recovery; therefore, another gc process in jvm regularly scans heap, Which scans heap based on the 4-Byte object address saved in the stack to locate these objects in heap, perform some optimization (such as merging idle memory blocks or something), and assume that the areas not scanned in heap are idle, all refresh (in fact, the useless objects with the object address lost in the stack are cleared), which is the process of garbage collection; for more information about garbage collection, see the previous article JVM Memory Model and garbage collection policy parsing in 51cto.

 
JVM Architecture

First, we need to figure out what data is and what commands are. Then we need to figure out the methods of the object and the attributes of the object respectively.

1) the method itself is part of the instruction operation code and is stored in the stack;

2) The internal variables of the method are used as the operands of the command. They are stored in the stack after the operation code of the command (in fact, simple types are saved in the stack, and object types are saved in the stack address, saves the value in heap). The preceding command operation code and command operations constitute a complete Java command.

3) The object instance includes its property value as data and is stored in the heap data zone.

Non-static object attributes are stored in heap as part of the object instance, and the object instance must be accessed through the address pointer saved in the stack. Therefore, whether an object instance can be accessed and its non-static attribute values depend entirely on whether the address pointer of the object instance in the stack can be obtained.

Differences between non-static and static methods:

Non-Static MethodThere is a major difference between static and non-static methods: non-static methods have an implicit input parameter, which is provided by JVM and has nothing to do with how we write code, this implicit parameter is the address pointer of the object instance in the stack. Therefore, non-static methods (instruction code in stack) can always find their own private data (Object attribute values in heap ). Of course, non-static methods must also obtain this implicit parameter. Therefore, before calling a non-static method, you must first create an object instance to obtain the address pointer in the stack, otherwise, the JVM will not be able to pass the implicit parameters to non-static methods.

Static MethodWithout this implicit parameter, you do not need a new object. As long as the class file is loaded into the JVM stack by classloader, the static method can be called. Of course, the static method cannot access the object attributes in heap at this time.

To sum up this process, when a class file is loaded into JVM by classloader load, the method commands are stored in the stack, and there is no data in the heap area. Then the program technician starts to execute commands. If it is a static method, the command code is executed in sequence. Of course, the command code cannot access the heap data zone. If it is a non-static method, an error is reported because the implicit parameter has no value. Therefore, before executing non-static methods, you must first create an object, allocate data in heap, and hand over the address pointer in the stack to non-static methods. In this way, the program technician executes commands in sequence, the command code can now access the heap data zone.

Static and dynamic attributes:

As mentioned above, both the object instance and dynamic attributes are stored in heap, and heap must be accessible by commands (class methods) only through the address pointer in the stack. Therefore, it can be inferred that static attributes are stored in the stack, but dynamic attributes are stored in heap. Because all commands and data in the stack are fixed, it is easy to calculate the offset. Therefore, no matter what commands (class methods ), can access the static attributes of the class. Because static attributes are stored in the stack, they have global attributes.

In JVM, static attributes are stored in the stack instruction memory area, and dynamic attributes are stored in the heap data memory area.

From: http://developer.51cto.com/art/201003/188753.htm

Related Article

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.