Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Stack and Heap in JVM

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to understand Stack and Heap in JVM, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

In JVM, memory is divided into two parts, Stack (stack) and Heap (heap). Here, we understand Stack and Heap from the perspective of the memory management principles of JVM, and recognize the problems of static methods and static properties in Java through these principles.

In general, the memory of JVM is divided into two parts: Stack and Heap.

Stack (stack) is the memory instruction area of JVM. Stack management is very simple, push a certain length of byte data or instruction, Stack pointer stack corresponding byte displacement; pop a certain byte length data or instruction, Stack pointer stack. Stack is fast, easy to manage, and the length of data or instruction bytes for each operation is known. So Java basic data types, Java instruction codes, and constants are all stored in Stack.

Heap (heap) is the memory data area of JVM. The management of Heap is very complex, allocating a variable amount of memory space at a time, dedicated to saving instances of objects. Allocating a certain amount of memory to save the object instance in Heap actually only saves the attribute value of the object instance, the type of the attribute and the type tag of the object itself, and does not save the method of the object (the method is instruction, saved in Stack). Allocating a certain amount of memory in Heap to save the object instance is similar to the serialization of the object. After the object instance is allocated in Heap, a 4-byte Heap memory address needs to be saved in Stack to locate the location of the object instance in Heap, so that the object instance can be found easily.

Because the memory management of Stack is sequential and fixed length, there is no memory recovery problem, while Heap allocates memory randomly and has the problem of memory allocation and recycling. So there is another GC process in JVM that scans Heap periodically. It scans Heap according to the addresses of 4-byte objects stored in Stack, locates these objects in Heap, makes some optimizations (such as merging free memory blocks, etc.), and assumes that areas that are not scanned in Heap are free, all refresh (actually clearing useless objects that have lost object addresses in Stack), this is the process of garbage collection. For a more in-depth explanation of garbage collection, please refer to 51CTO's previous article, "JVM memory model and garbage collection policy parsing."

The architecture of JVM

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

1) the method itself is the opcode part of the instruction and is saved in Stack

2) the internal variable of the method, as the Operand part of the instruction, is stored in Stack after the Operand of the instruction (in fact, the simple type is saved in Stack, the object type is stored in Stack, and the value is saved in Heap); the above instruction opcode and instruction Operand constitute a complete Java instruction.

3) the object instance includes its attribute values as data, which are saved in the data area Heap.

Non-static object properties are saved in Heap as part of an object instance, which must be accessed through an address pointer stored in Stack. Therefore, access to the object instance and its non-static property values depends entirely on getting the address pointer of the object instance in the Stack.

The difference between non-static and static methods:

Non-static methods have a significant difference from static methods: non-static methods have an implicit incoming parameter that is given to it by JVM, regardless of how we write the code. The implicit parameter is the address pointer of the object instance in Stack. So non-static methods (instruction code in Stack) can always find their own dedicated data (object property values in Heap). Of course, non-static methods must also obtain the implicit parameters, so before non-static methods are called, they must first new an object instance and get the address pointer in Stack, otherwise JVM will not be able to pass the implicit parameters to non-static methods.

Static methods do not have this implied parameter, so they do not need a new object, and can be called as long as the class file is entered into JVM's Stack by ClassLoader load. Of course, static methods cannot access object properties in Heap at this time.

To sum up the process: when a class file is entered into JVM by ClassLoader load, the method instructions are saved in Stack, and there is no data in the Heap area. Then the program technician starts to execute the instruction, if it is a static method, it directly executes the instruction code in turn, of course, the instruction code cannot access the Heap data area; if it is a non-static method, it will report an error because the implied parameter has no value. Therefore, before the non-static method is executed, it is necessary to new the object, allocate the data in the Heap, and give the address pointer in the Stack to the non-static method, so that the program technician executes the instructions in turn, and the instruction code can access the Heap data area at this time.

Static and dynamic properties:

As mentioned earlier, object instances and dynamic properties are stored in Heap, and Heap must be accessed by instructions (methods of the class) through the address pointer in Stack. So it can be inferred that static properties are stored in Stack, unlike dynamic properties are stored in Heap. Because they are all in Stack, and the instructions and data in Stack are of fixed length, it is easy to calculate the offset, so the static properties of the class can be accessed no matter what instruction (the method of the class). Also because static properties are stored in Stack, they have global properties.

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

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report