Stack memory heap and stack memory stack in Java Virtual machine

Source: Internet
Author: User

Original link: http://www.cnblogs.com/laoyangHJ/archive/2011/08/17/gc-Stack.html

Drill down into Java Virtual machines: stacks and heap in the JVM

In the JVM, memory is divided into two parts, stack (stack) and heap (heap), where we know the stack and heap from the perspective of the memory management principle of the JVM, and these principles recognize the problem of static and static properties in Java.

In general, the JVM's memory is divided into two parts: Stack and heap.

  The stack (stack) is the memory instruction area of the JVM. Stack management is simple, push a certain length byte of data or instructions, stack pointer pressure stack corresponding byte displacement, pop a certain byte length data or instructions, stack pointer stack. The stack is fast, management is simple, and the data or instruction byte length of each operation is known. So Java basic data types, Java instruction code, constants are stored in the stack.

  The heap is the memory data area of the JVM. Heap management is complex and allocates an indefinite amount of memory space each time, specifically to hold an instance of the object. Allocating a certain amount of memory in the heap to hold an object instance is actually just a matter of saving the property value of the object instance, the type of the property and the type tag of the object itself, and not the method of saving the object (by instruction, saved in the stack), in the heap Allocating a certain amount of memory in a saved object instance is similar to serializing an object. When an object instance is allocated in the heap, a 4-byte heap memory address is stored in the stack, which locates the object instance in the heap, making it easier to locate the object instance.

Because Stack's memory management is allocated sequentially, and there is no memory recovery problem, heap is randomly allocated memory, indefinite length, there is memory allocation and recovery problem, so there is another GC process in the JVM, periodically scans the heap, It scans the heap based on the 4-byte object address stored in the stack, locates those objects in the heap, makes some optimizations (such as merging blocks of free memory, etc.), and assumes that the areas that are not scanned in the heap are idle. All refresh (actually clears the useless object that lost the object's address in the stack), which is the garbage collection process; For a more in-depth explanation of garbage collection, refer to the "JVM memory model and garbage collection policy Resolution" article before 51CTO.


The architecture of the JVM

The first thing we need to figure out is what the data is and what the instructions are. Then you need to figure out where the object's methods and object properties are stored separately.

1) The method itself is the operation code part of the instruction, which is stored in the stack;

2) The internal variables of the method are part of the operand of the instruction, followed by the instruction opcode, saved in the stack (in fact, the simple type is saved in the stack, the object type holds the address in the stack, and the value is stored in the heap); the instruction opcode and the instruction operand above constitute the complete Java Instructions.

3) The object instance includes its property values as data and is saved in the data area heap.

Non-static object properties are persisted as part of the object instance in the heap, and the object instance must be accessed through an address pointer saved in the stack. Therefore, the ability to access an object instance and its non-static property values depends entirely on the availability of an object instance's address pointer in the stack.

  The difference between a non-static method and a static method:

There is a significant difference between a non-static method and a static method: a non-static method has an implicit incoming parameter, which is given to it by the JVM, regardless of how we write the code, and this implicit argument is the address pointer of the object instance in the stack. Therefore, a non-static method (the instruction code in the stack) can always find its own private data (object property values in the heap). The non-static method must also obtain the implied argument, so the non-static method must first new an object instance to obtain the address pointer in the stack before the call, otherwise the JVM will not be able to pass the implied parameter to the non-static method.

The static method does not have this implicit argument and therefore does not require a new object, which can be called as long as the class file is ClassLoader load into the JVM's stack. Of course, at this point the static method is not accessible to object properties in the heap.

Summarize the process: When a class file is ClassLoader load into the JVM, the method instructions are saved in the stack, where the heap has no data. Then the program technology starts executing the instruction, if it is a static method, executes the instruction code directly, of course, the instruction code is not accessible to the heap data area, if the non-static method, because the implied parameter has no value, will be an error. Therefore, before the non-static method executes, the new object is assigned data in the heap, and the address pointers in the stack are given to non-static methods, so that the program technology executes the instructions in turn, and the instruction code can now access the heap data area.

  Static properties and Dynamic properties:

The previously mentioned object instances and the dynamic properties are all stored in the heap, and the heap must be accessed by the instruction (the method of the class) through the address pointers in the stack. It is therefore possible to infer that static properties are stored in the stack, unlike dynamic properties stored in the heap. Because all are in the stack, and the instructions and data in the stack are fixed-length, it is easy to calculate the offset, and therefore, regardless of the instruction (the method of the Class), you can access the static properties of the class. It is also because the static property is stored in the stack, so it has a global property.

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

Stack memory heap and stack memory stack in Java Virtual machine

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.