Talking about static and static properties of Java classes from the perspective of JVM memory management

Source: Internet
Author: User

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. (both stack and heap are in RAM)

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 , not the method of saving the object (the method is instruction, saved in the stack), Allocating a certain amount of memory in the heap is similar to the serialization of object instances and objects.

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. This is the garbage collection process, where all the refresh (in effect clears the useless object that lost the object's address in the stack).

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

For the sake of description, I simply collectively:

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 as the operand part of the instruction, followed by the operation code of the instruction, saved in the stack (the simple type is saved in the stack, the object type holds the address in the stack, and the value is saved in the heap);

The above instruction opcode and instruction operand constitute the complete Java instruction.

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.

First analyze 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.

Again, 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 inferred that the static property is stored in the stack (the base type is saved in the stack, the object type address is saved in the stack, the value is saved in the heap), and the dynamic attribute is saved 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.

To summarize: Static properties are saved in the stack instruction memory area, and dynamic properties are stored in the heap data memory area.

Reprint Link:

Http://www.189works.com/article-58284-1.html

http://blog.csdn.net/zhongyili_sohu/article/details/8031410

Talking about static and static properties of Java classes from the perspective of JVM memory management

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.