In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the memory structure of the Java virtual machine". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the memory structure of the Java virtual machine is.
One: brief introduction
Memory (Memory), also known as internal memory, is used to temporarily store operational data in CPU and data exchanged with external memory such as hard disk. As long as the computer is running, CPU will transfer the data that needs to be operated to memory for operation, and when the operation is completed, CPU will transmit the results.
The Java virtual machine divides the memory it manages into several different data regions during the execution of the Java program. These areas have their own uses and the time of creation and destruction, some areas exist with the start of the virtual machine process, and some areas are established and destroyed depending on the start and end of the user thread.
Process: the execution of a program, which is a running activity of a program with certain independent functions about a data set. It is the basic unit of dynamic execution of the operating system. In the traditional operating system, the process is not only the basic allocation unit, but also the basic execution unit.
A process is an entity. Each process has its own address space, which generally includes text areas (text region), data areas (data region), and stacks (stack region). The text area stores the code executed by the processor; the data area stores variables and dynamically allocated memory used during process execution; and the stack area stores instructions and local variables for active procedure calls. Second, a process is an "executing program". A program is an inanimate entity, and only when the processor gives life to the program can it become an active entity, which we call a process.
A process has three states, ready, running, and blocking. The ready state is actually getting all the resources outside the cpu, which can be executed immediately as long as the processor allocates resources. There is a queuing sequence in the ready state, and the queuing principle will not be repeated. The runtime gets the resources allocated by the processor and the program begins to execute. Blocking state, when the program condition is not enough, it needs to wait for the condition to be satisfied before it can be executed. For example, when waiting for the iUnix operation, the current state is called blocking state.
Threads: usually there can be several threads in a process, of course, there is at least one thread in a process, otherwise there is no point in existence. The thread can make use of the resources owned by the process. In the operating system that introduces the thread, it usually takes the process as the basic unit of allocating resources, and the thread is regarded as the basic unit of independent operation and scheduling. Because the thread is smaller than the process, basically does not have system resources, so the cost of its scheduling will be much less, which can more efficiently improve the degree of concurrent execution among multiple programs in the system.
The main difference between processes and threads is that they are different ways of managing operating system resources. A process has an independent address space. After a process crashes, it will not affect other processes in protected mode, and threads are just different execution paths in a process. Threads have their own stack and local variables, but there is no separate address space between threads, so the death of a thread equals the death of the whole process, so a multi-process program is more robust than a multi-thread program, but when a process is switched, it consumes more resources and is less efficient.
Two: program counter
A program counter is a small piece of memory space that can be seen as an indicator of the bytecode line number executed by the current thread.
When the bytecode interpreter works, it selects the next bytecode instruction to be executed by changing the value of this counter; basic functions such as branching, looping, jumping, exception handling, thread recovery and so on depend on this counter.
When the thread is in the execution process, CPU will execute the fetch instructions according to the contents stored in the program counter. When CPU fetches the current instruction, CPU automatically modifies the contents of the program counter to point to the line number of the next instruction bytecode. Because the program counter stores numeric values, it does not expand the demand space as the program runs, so there is no overflow.
Suppose thread An is executing, and when execution reaches a certain stage, higher priority thread B executes. At this point, thread A hangs and thread B executes. When thread B finishes execution, thread A needs to be awakened to continue execution, so how to continue execution from the interrupt position of thread A requires CPU to access thread A's program counter to obtain the next execution instruction to ensure that the program continues to execute. Because each thread needs to save its own execution location, it makes the program counter private to the thread.
Native native methods are mostly implemented in C and are not compiled into bytecode instructions that need to be executed, so it is undefined in the counter
This memory area is the only one that does not specify any OutOfMemoryError in the java virtual boundary specification.
Three: Java virtual machine stack
It is a last-in-first-out stack in which the saved elements are stack frames. It is also thread private, and its life cycle is the same as that of threads.
When the program is running, each call to a method generates a stack frame, and presses the stack frame of the currently executing method into the virtual machine stack. The stack frame at the top of the virtual machine stack is the current active stack frame. Since the stack frame is created when the method is called, its saved content must be closely related to the method.
Local variable scale: a local variable table is a set of local variable value storage space for storing method parameters and local variables defined within the method. The local variable table stores a variety of basic data types, object references and returnAddress types known to the compiler. The memory of 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.
Operand stack: an Operand stack is an array in word length. But it is not accessed through an index, but through standard stack operations-stack pressing and stack exiting. For example, if an instruction A pushes a value 1 into the Operand stack, instruction B pushes the value 2 into the stack, and instruction C later pops up the two values for addition and pushes the result 3 into the stack. Some operations in the method can be completed through the Operand stack.
Dynamic linking: each stack frame contains a reference to the pool of runtime variables of the type in which the current method is located to dynamically link the code of the current method. In the class file, if a method wants to call another method or access its member variables, it needs to be represented by symbolic references, so the function of dynamic linking is to parse these methods or variables represented by symbolic references into direct references at the appropriate time.
Return address: generally speaking, when the method exits normally, the value of the caller's PC counter can be used as the return address, and this counter value is likely to be saved in the stack frame. When the method exits abnormally, the return address is determined by the exception handler, and this information is generally not saved in the stack frame. The process of exiting a method is actually equivalent to taking the current stack frame off the stack, so the operations that may be performed when exiting are: restoring the local variable table and Operand stack of the upper method, pressing the return value (if any) into the Operand stack of the calling stack frame, calling the value of the PC counter to point to an instruction after the method calling instruction, and so on.
Other information: the virtual machine specification allows a specific virtual machine implementation to add some information not described in the specification to the stack frame, such as highly relevant information, which depends entirely on the specific virtual machine implementation.
3. If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError exception will be thrown; if the virtual machine stack can be dynamically expanded, and if enough memory cannot be applied for, an OutOfMemoryError exception will be thrown.
Four: 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 for the virtual machine, while the local method stack serves the Native methods used by the virtual machine.
The local method stack area also throws StackOverflowError and OutOfMemoryError exceptions.
Five: Java heap
The Java heap is an area of memory shared by all threads and is created when the virtual machine starts. The sole purpose of this memory area is to store object instances, where almost all object instances allocate memory.
Java reactor is generally divided into three parts: Cenozoic, old and permanent.
New generation: mainly used to store new objects. It generally occupies 1 stroke and 3 spaces of the heap. Due to the frequent creation of objects, the new generation will frequently trigger MinorGC for garbage collection.
Old age: mainly stores memory objects with a long life cycle in an application. In the old days, objects were relatively stable, so MajorGC was not executed frequently. Generally speaking, a MinorGC is carried out before MajorGC, which makes a new generation of objects enter the old age, which is triggered when there is not enough space. When you cannot find enough contiguous space to allocate to a newly created larger object, it will also trigger a MajorGC for garbage collection in advance to make room.
Permanent generation: refers to the permanent storage area of memory, which mainly stores Class and Meta (metadata) information. Class is placed in the permanent area when loaded. Unlike the area where the instance is stored, GC does not clean up the permanent area while the main program is running. So this also causes the region of the permanent generation to swell up as more Class is loaded, eventually throwing an OOM exception.
The Java heap is the main area managed by the garbage collector.
The Java heap can be in physically discontiguous memory space, as long as it is logically continuous.
It can be extended with-Xmx and-Xms. If it cannot be extended, an OutOfMemoryError exception will be thrown.
Six: method area
The method area, like the Java heap, is a memory area shared by each thread. It is used to store class information, constants, static variables, compiled code and other data that have been loaded by the virtual machine.
When the method area cannot meet the memory allocation requirements, an OutOfMemoryError exception will be thrown
At this point, I believe you have a deeper understanding of "what is the memory structure of the Java virtual machine". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.