In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the principle and application of jvm memory structure. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the principle and application of jvm memory structure.
Overview of jvm memory structure
The memory structure of jvm mainly includes: method area, heap, virtual machine stack, local method stack, program counter and so on. Let's take a look at each part in detail.
1.1 Virtual Machine Stack
Function:
The virtual machine stack describes the dynamic memory model of java method execution, and the calling structure of the method is realized by entering and leaving the stack.
Analysis:
stack structure is characterized by last-in, first-out, method call process is also called after the method returns the result, the method first called and then executed, you are dry wood, I am the fire. You are the fish and I am the river. (it is not known here whether the egg hatched the chicken or the chicken laid the egg.)
takes methodA calling methodB as an example to take a look at the execution process of the method stack
First of all, the main method enters the empty stack (this step is avoided in the figure). The main method calls methodA-- "methodA stack--" methodA calls methodB-- "methodB stack--" methodB completes execution, leaves the stack and returns the return value-- "methodA execution is complete, exit the stack and return the return value--" the main thread is finished executing and leaving the stack (this step is avoided in the figure)-- "the program execution is complete.
, so what exactly does methodA and methodB contain?
, we might as well think about it from the perspective of method definition. When writing a program, there are several elements that often appear: variables, control structures (loops, branches), function calls (also called methods). Functions actually operate variables with a certain control structure, interspersed with calls to other functions, and the branch structure mainly controls which line the program executes. This is implemented by the program counters we will talk about in the next 1.5 chapters. The function call level is supported by the virtual machine stack as mentioned above, and the exit of variables and methods is the main content of methodA and methodB.
methodA and methodB are called stack frames in the virtual machine stack. The stack frames include variables (local variable table-basic type, object reference-object memory address, etc.) and return value address and other information. It can be seen that the content contained in the stack frame can be determined by the compiler. When entering a method, how much local variable space needs to be allocated is completely determined, and the size of the local variable table will not be changed during the run time.
Possible exceptions in this area:
The stack has a depth, and when the level of method calls exceeds the depth of the stack, there will be StackoverflowError, which is easy to occur when recursive calls are made and when the baseline conditions are unreasonable.
When the depth of the stack increases, the required local variable space also increases, and when it exceeds a certain limit, an OutOfMemoryError exception occurs.
1.2 Local method stack
Function:
The difference between the local method stack and the virtual machine stack is that different types of methods are called. The local method stack is used to record the call of the native method. The local method stack does not specify the native method implementation language and data structure, which is mainly defined and implemented by the virtual machine itself.
Note:
Because the functions of the local method stack and the virtual machine stack are very similar, these two areas are merged into one region in the implementation of some virtual machines, such as the more commonly used Hotspot.
Possible exceptions in this area
Like the virtual machine stack, it is possible for StackoverflowError or OutOfMemoryError to appear in this area.
1.3 heap
Function:
The heap is the largest block of memory managed by jvm, and this area has only one function to hold object instances. (java is an object-oriented language-- "heap is used to store object instances--" the heap takes up the most space, well, nothing wrong with it. )
Note:
Since this area takes up the most memory space, and objects are created and destroyed frequently, what if there is not enough memory space? It's the same as killing pigs and picking the fattest slaughters. this area is the main area for garbage recycling. Java saves us the work of manually releasing memory in the code layer and automates memory collection through the garbage collector ("7 days proficient in java": "21 days C++ from entry to burial", you know the importance of automatic garbage collection)
Possible exceptions in this area:
When you allocate space for an object, you run out of memory and OutOfMemoryError (knock on the blackboard) occurs.
Corresponding troubleshooting strategy: configure the parameter-XX:+HeapDumpOnOutOfMemoryError, so that when this exception occurs, the virtual machine will generate a snapshot of the memory heap. Using the tool Eclipse Memory Analyzer or jprofiler, you can analyze the percentage of space occupied by some objects and quickly locate the problem. If there is a problem with the code, such as constantly creating some objects, but not releasing them, then the problem should be solved by modifying the code, if there is no problem with the code. You need to increase the heap size by increasing the physical memory and adjusting the jvm heap parameters. (- Xms heap initialization size,-Xms maximum space size, which is generally set to the same size in the actual configuration process to avoid memory jitter and affect stability)
1.4 method area
Function:
The method area is mainly used to access class information, constants, static variables, running constant pools, and so on.
Analysis:
The runtime constant pool is used to access literals and symbolic references. When the String variable is declared in double quotes, this variable is not opened to the heap space, but to the runtime pool in the method area. The runtime constant pool can be generated not only at compile time, but also dynamically during program running, such as the intern method in String. It is in the running state of the program that the string value is put into the run-time quantity pool.
Possible exceptions in this area:
OutOfMemoryError exceptions can also occur in this area when there is insufficient memory.
1.5 Program counter
Function:
The program counter is mainly used to record the line number indicator of the bytecode executed by the current thread, that is, which line the current thread is executed to. Because the function is relatively simple, compared with other areas, the memory space is very small.
Possible exceptions in this area:
The role of the program counter determines that exceptions caused by the program itself will not occur in this area, and if the exception occurs here, it is a design flaw in jvm itself.
Note:
If the thread executes the java method, the counter corresponds to the address of the bytecode instruction, but if it is the native method, the underlying layer is not implemented by java, and the value of the counter is undefined
Extension:
When it comes to program counters, in some programming languages, the goto keyword is supported, goto allows the program to jump directly to the specified line, java does not support goto, but goto is taken as a reserved word, that is, java does not support this syntax, but it does not allow developers to define it as a variable to prevent the subsequent development of java, after adding the goto keyword, some programs run abnormally.
1.6 Thread shared area and Thread exclusive area
Of the five areas above the , the method area and the heap are areas shared by threads, and the content placed in these two areas is independent of the thread, and either thread may access the content in these two areas.
for each thread, its calling method stack should belong to a thread alone. If multiple threads share a method stack, the execution order of the program cannot be guaranteed, and which line the thread executes should also be unique, so the method stack and program counter are exclusive to the thread.
Thank you for reading, the above is the content of "the principle and application of jvm memory structure". After the study of this article, I believe you have a deeper understanding of the principle and application of jvm memory structure, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.