In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about what the memory structure of JVM is like in the Java virtual machine. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. JVM launch process:
When JVM starts, it is started by the java command / javaw command.
2. Basic structure of JVM:
JVM basic structure diagram:
The description in "in depth understanding the Java Virtual Machine (second Edition)" looks like this:
Memory allocation in Java:
When the Java program is running, it needs to allocate space in memory. In order to improve the operation efficiency, the data is divided into different spaces, because each area has a specific way of processing data and memory management.
It is divided into the following 5 memory spaces: (very important)
Stack: storing local variables
Heap: store everything that comes out of new
Method area: class information, constants, static constants, etc. loaded by the virtual machine.
Program counter (system related)
Local method stack
1. Program counter:
Each thread has one PC register
Created when a thread is created
Point to the address of the next instruction
When a local method is executed, the value of PC is undefined
2. Method area:
Save loaded class information
Constant pool of type
Fields, method information
Method bytecode
Usually associated with the Perm
3. Heap memory:
It is closely related to program development.
Application system objects are stored in the Java heap
All threads share the Java heap
For generational GC, heap is also generational.
Main areas managed by GC
Today's GC basically uses generation collection algorithm, if it is generation, then the heap is also generation by generation. If the heap is generational, the heap space should look like this:
The above figure is the basic structure of the heap, which will be explained in more detail in a later article.
4. Stack memory:
The thread is private and has the same life cycle as the thread
The stack consists of a series of frames (so the Java stack is also called the frame stack)
The frame holds the local variables, Operand stack, and constant pool pointer of a method.
Each method call creates a frame and presses the stack
Explanation:
The Java virtual machine stack describes the memory model of Java method execution: when each method is called, a stack frame is created to store local variables, operation stacks, dynamic links, method exits and other information. The process that each method is called until the completion of execution corresponds to the process of a stack frame from stack to unstack in the virtual machine.
In the Java virtual machine specification, two exceptions are specified for this area:
(1) if the stack depth of the thread request is too deep, beyond the depth allowed by the virtual machine, StackOverFlowError (such as infinite recursion) will occur. Because each stack frame takes up a certain amount of space, and Xss specifies the maximum stack space, if you exceed this value, an error will be reported.
(2) the virtual machine stack can be expanded dynamically. If the virtual machine stack is expanded to the extent that it cannot apply for enough memory space, OOM will appear.
4.1Local variables of Java stack: including parameters and local variables
The local variable table stores the basic data type, object reference, and returnAddress type (the address that points to a bytecode instruction). Of these, 64-bit data of long and double types takes up 2 local variable spaces (slot), while the rest of the data types occupy only 1. The memory space required by the local variable table is allocated during compilation.
For example, I wrote the following code:
1 package test03; 2 3 / * * 4 * Created by smyhvae on 2015-8-15. 5 * / 6 public class StackDemo {7 8 / / static method 9 public static int runStatic (int I, long l, float f, Object o, byte b) {10 return 0; 11} 12 13 / / instance method 14 public int runInstance (char c, short s, boolean b) {15 return 0; 16} 17 18}
In the above code, the static method has 6 parameters and the example method has 3 parameters. The corresponding local variables are as follows:
In the table above, the local variables corresponding to the static method and the example method are basically similar. However, there are the following differences: in the table of the instance method, the first place is the reference to the current object.
4. 2 the function calls of the Java stack form the stack frame:
A stack frame is created each time the method is called, such as the following method:
Public static int runStatic (int iMagneL float fje object o, byte b) {return runStatic (iMagne ldre freco o o b);}
Each time it is called, a frame is created, and when the method call ends, the frame goes off the stack. As shown in the following figure:
4.3Operand stack of Java stack
Java does not have registers, and all parameters are passed using the Operand stack
For example, the following code:
Public static int add (int a drawing int b) {int center0; return c;}
The steps to press the stack are as follows:
0: iconst_0 / / 0 stack
1: istore_2 / / pop-up int, stored in local variable 2
2: iload_0 / / Stack the local variable 0
3: iload_1 / / Local variable 1 stack
4: iadd / / Pop up 2 variables, sum, and press the stack
5: istore_2 / / pop-up result, put in local variable 2
6: iload_2 / / Local variable 2 stack
7: ireturn / / return
If you calculate the value of 100 to 98, the change in the Operand stack is shown in the following figure:
4.4 allocation on the stack of Java stack:
Small objects (usually dozens of bytes) can be directly assigned to the stack without escape.
Directly allocated on the stack, can be automatically recycled, reducing the pressure of GC
Large objects or escaping objects cannot be allocated on the stack
Stack, heap, method area interaction:
Third, memory model:
Each thread has one working memory. Working memory and main memory are independent. Working memory stores a copy of the value of a variable in main memory.
When data is copied from the main memory to the working memory, two actions must occur: first, the read operation performed by the main memory; second, the corresponding load operation performed by the working memory; and when the data is copied from the working memory to the main memory, there are also two operations: the first, the store operation performed by the working memory; and the corresponding write operation performed by the main memory.
Every operation is atomic, that is, it will not be interrupted during execution.
For ordinary variables, the updated values in one thread cannot be immediately reflected in other variables. If you need to be immediately visible in other threads, you need to use the volatile keyword as the identity.
1. Visibility:
One thread modifies the variable, and other threads can know immediately
Ways to ensure visibility:
Volatile
Synchronized (before unlock, write the variable value back to main memory)
Final (once initialization is complete, other threads are visible)
2. Order:
Within this thread, the operations are orderly.
Looking outside the thread, the operations are all out of order. (instruction rearrangement or main memory synchronization delay)
3. Rearrange instructions:
Instruction rearrangement: destroys the order between threads:
Instruction rearrangement: a method of ensuring order:
Basic principles of instruction rearrangement:
Program order principle: ensuring semantic serialization within a thread
Volatile rule: the writing of volatile variables occurs first in the read
Lock rule: unlocking (unlock) must occur before subsequent locking (lock)
Transitivity: a precedes Bmai B before C, then A must precede C.
The thread's start method precedes each of its actions
All operations of the thread precede the termination of the thread (Thread.join ())
The interrupt of the thread (interrupt ()) precedes the code of the interrupted thread
The execution of the constructor of the object ends before the finalize () method
4. Explain the concept of running and compiling
Explain the operation:
Interpret execution to run bytecode in an interpreted manner
Explain the meaning of execution: read and execute a sentence.
Compile run (JIT):
Compile bytecode into machine code
Execute machine code directly
Run-time compilation
There is an order of magnitude improvement in performance after compilation
The performance of compilation runs is better than that of interpretive runs.
Thank you for reading! This is the end of this article on "what is the memory structure of JVM in the Java virtual machine?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.