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--
How to analyze JVM memory structure and 6 large areas, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.
In fact, for our general understanding of computer memory, it is the most frequent area of CPU and computer interaction, all data is first through the hard disk to memory, and then by the CPU to obtain data from memory for processing, and save the data to memory, through paging or fragmentation technology to flush the data in memory to the hard disk. What is the memory structure of JVM? JVM as a platform running on the operating system, but independent of OS running, its memory should include at least such as registers, stacks and other areas.
The JVM divides data into six regions for storage at runtime, not just the well-known Heap region, as shown below:
JVM memory allocation structure diagram
The following describes what each of the following regions does and what they do.
PC Register
PC register is a small memory area, mainly used to record the line number of byte code executed by the current thread. Bytecode interpreters work by changing the program counter of the current thread to select the next bytecode instruction. Any branches, loops, method calls, judgments, exception handling, thread waiting and recovery threads, recursion, etc. are done through this counter.
Since Java multithreading is implemented by alternating threads and allocating processor time, only instructions in one thread are executed in a core of the processor at any given time. Thus, in order for a thread to wait until it has finished executing and needs to resume execution at the correct location, each thread has a separate program counter that records the line number of the current instruction. Counters are independent of each other and do not affect each other. We call this memory "thread private" memory.
If the method called is native, no information is stored in the PC registers.
l JVM stack
JVM stack is thread private, each thread will create JVM stack at the same time, JVM stack stored for the current thread local basic type variables There are eight basic types defined in Java: boolean, char, byte, short, int, long, float, double), partial return results, and Stack Frame, objects of non-primitive types have only one address on the JVM stack pointing to the heap, so variables of primitive types in Java are value passing, while variables of non-primitive types are reference passing. Space for the JVM stack in Sun JDK implementations is allocated on physical memory, not from the heap.
Because the JVM stack is thread-private, it is very efficient in allocating memory, and this memory is automatically reclaimed when the thread runs out.
StackOverflowError is thrown when the JVM stack is out of space. In Sun JDK, the stack size can be specified by-Xss, for example, the following code:
new Thread(new Runnable(){ public void run() { loop(0); } private void loop (int i){ if(i!= 1000){ i++; loop (i); } else{ return; } } }).start();
When the JVM parameter is set to-Xss 1K, it will report an error similar to the following:
Exception in thread "Thread-0"java.lang.StackOverflowError
l Heap
Heap is the most familiar area. It is the area used by JVM to store object instances and array values. It can be considered that the memory of all objects created by new in Java is allocated here. The memory of objects in Heap needs to wait for GC to recycle. Heap ** is 2G on 32-bit operating systems, and there is no limit on 64-bit operating systems. Its size is controlled by-Xms and-Xmx. -Xms is the minimum Heap memory requested when JVM starts. The default is 1/64 of physical memory but less than 1G. -Xmx is the ***Heap memory that JVM can request. The default is 1/4 of physical memory. By default, when the free heap memory is less than 40%, JVM will increase the Heap size to the specified size of-Xmx. This ratio can be specified by-XX:MinHeapFreeRatio=. When the free heap memory is greater than 70%, JVM will adjust the Heap size to the specified size of-Xms. This ratio can be specified by-XX:MaxHeapFreeRatio=. However, for running systems, in order to avoid frequent Heap Size, the values of-Xms and-Xmx are usually set to the same, so these two parameters for adjusting the ratio are usually useless. In fact, jvm has more elaborate designs for heap memory allocation, use, management, collection, etc., which can be detailed in JVM heap memory analysis.
OutOfMemory error messages are thrown when the heap needs more memory than it allows.
Method Area (Method Area)
The method area stores the information of the loaded class (name, modifier, etc.), static variables in the class, constants defined as final type in the class, Field information in the class, and method information in the class. When developers obtain information through getName, isInterface, etc. in the Class object in the program, these data all come from the method area, which shows the importance of the method area. Similarly, the method area is globally shared, it is GC under certain conditions when the virtual machine starts, and when the method area needs to use more memory than it allows, it will throw an OutOfMemory error message.
In Sun JDK, this area corresponds to PermanetGeneration, also known as Persistent Generation, which defaults to 64M and can be specified by-XX:PermSize and-XX:MaxPermSize.
Runtime Constant Pool
Similar to the symbol table in C, it stores fixed constant information in the class, reference information of methods and Fields, etc., and its space is allocated from the method area. The constant pool for a class or interface is allocated when the class file for that class is successfully loaded by the java virtual machine.
Native Method Stacks
The JVM supports native method execution with a native method stack, an area that stores the state of each native method call.
For example, there is this code:
public class A { public static void main(String[]args){ String a="a"; String b="b"; String ab="ab"; System.out.println((a+b)==ab); // false System.out.println(("a"+"b")==ab); // true final String afinal="a"; String result=afinal+"b"; System.out.println(result==ab); // true String plus=a+"b"; System.out.println(plus==ab); // false System.out.println(plus.intern()==ab); // true } }
Analyze the results of the above code execution, you can use javap-verbose A to assist in understanding the analysis.
l (a+b)==ab
A+b is the sum of two variables, and its value cannot be determined until runtime, when the JVM produces a new object for the sum of the two, so a+b=ab results in false.
l ("a"+"b")==ab
"a"+"b" is a constant, which the JVM has converted to an "ab" string at compile time, and ab="ab" is also a constant, which is the same address in the constant pool, so ("a"+"b")==ab is true.
l result==ab
result=afinal+"b", afinal is a final variable, result has also been converted to "ab" at compile time, and "ab" is also the same address in the constant pool, so result =ab is true.
l plus=ab
The case for plus and a+b is the same, so plus==ab is false.
l plus.intern()==ab
The difference here is that plus.intern() is called, which gets the constant pool address that plus points to, so plus.intern()==ab is true.
After mastering the JVM object memory allocation mechanism, let's take a look at how the JVM achieves automatic object memory collection, which refers to the collection of Heap and Method Area, and the collection of other areas is simply managed by the JVM according to its life cycle.
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to 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.
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.