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 Java Runtime data Zone in Java Virtual Machine

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to understand the Java runtime data area in the Java virtual machine. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

Java virtual machine runtime datazone

Dark blue on the left is a common area, and light blue on the right is a private area.

From the diagram, we can divide Java memory into heap memory (Heap) and stack memory (Stack). Although this method is rough and actually much more complex, for beginners, these are the two areas that we are most concerned about.

Total memory = heap memory (Xmx) + method area memory (MaxPermSize) + stack memory (Xss) * number of threads + direct memory (MaxDirectMemorySize, out of heap) + virtual machine memory

Program counter

A program counter is a small piece of memory space that can be seen as a line number indicator of the bytecode executed by the current thread. In the conceptual model of the virtual machine, the bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to be executed. branch, loop, jump, exception handling, thread recovery and other basic functions all rely on this counter.

Friends who have studied assembly should be easier to understand. In popular terms, it stores the number of the next piece of code to be executed.

The multithreading of Java virtual machine is realized by thread switching and allocating processor execution time in turn. At any given time, a processor will only execute instructions in one thread. Therefore, in order to return to the correct execution position after thread switching, each thread needs to have a separate program counter.

Each thread has its own program counter, which does not affect the memory that belongs to the "thread private".

If the thread is executing a Java method, the program counter records the address of the virtual machine bytecode instruction being executed; if the Native method is being executed, the counter value is empty.

This memory region is the only one that does not specify any OutOfMemoryError conditions in the Java virtual machine specification.

Native Method (native methods), a native method is an interface that uses Java to call non-Java code, and the implementation of the method is implemented by non-Java methods such as C and C++. It needs to be noted here, because calling non-Java methods also involves GC and OOM.

Java virtual machine stack

The Java virtual machine stack is also thread-private and has the same life cycle as threads. Virtual machine stack describes the memory model of Java method execution. When each method is executed, a stack frame (Stack Frame) is created to store local variables, operation stack, dynamic link, method exit and other information.

When the thread calls the Java method, the virtual machine pushes a new stack frame into the corresponding thread's virtual machine stack; when the method returns, the stack frame is popped out of the stack and discarded.

The memory space required by the local variable table is allocated during compilation. When entering a method, how much local variable space needs to be allocated in the frame is completely determined, and the size of the local variable table will not be changed during the operation of the method.

There are two kinds of exceptions in this area: if the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError exception is thrown; if the virtual machine stack can be dynamically expanded and enough memory cannot be applied for, an OutOfMemoryError exception is thrown.

I would also like to say a digression here. Since the entry and return of each method corresponds to the press-in and pop-up of the stack frame, this process takes a certain amount of time and resources, so it also means that the more methods are called in the code, the lower the execution efficiency. Can we not split it up without breaking it up?

Local method stack

The role of the local method stack is very similar to that of the virtual machine stack, except that the virtual machine stack performs Java methods (that is, bytecode) services for the virtual machines, while the local method stacks serve the Native methods used by the virtual machines.

Java reactor

The Java Java Heap is an area shared by all threads, and all object instances and arrays are allocated on the heap.

The Java heap is the main area managed by the garbage collector. From the perspective of memory recovery, because the current collectors basically use generation-by-generation collection algorithms, the Java heap can also be subdivided into: the new generation and the old age; the new generation is more detailed, such as Eden space, From Survivor space, To Survivor space and so on.

If there is no memory in the heap to complete the instance allocation, and the heap can no longer be expanded, an OutOfMemoryError exception will be thrown.

Method area

The method area is the memory area shared by each thread, which is used to store class information, constants, static variables, compiled code and other data that have been loaded by the virtual machine.

The goal of memory collection in this area is mainly for constant pools and the unloading of types.

When the method area fails to meet the memory allocation requirements, an OutOfMemoryError exception is thrown.

Running constant pool

The run-time pool is part of the method area. In addition to the description of the version, field, method, interface and other information of the class, there is also a constant pool of information in the CLass file, which is used to store all kinds of literals and symbolic references generated during compilation. This part of the content will be stored in the runtime pool of the method area after the class is loaded.

Direct memory

It is not part of the virtual machine runtime data zone, nor is it a memory area defined in the JAVA virtual machine specification. The NIO class is added to JDK1.4, which introduces a way of Buffer based on Channel. It can use the Native function library to allocate out-of-heap memory directly, and then operate through a DirectByteBuffer object stored in the JAVA heap as a reference to this memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth between the JAVA heap and the Native heap.

This is how to understand the Java runtime data area in the Java virtual machine shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report