Interviewer : today to talk about the memory structure of the JVM, right?
candidate : Well, good
candidate : I mentioned in the previous interviews that the class file will be loaded into the JVM by the class loader, and the JVM will be responsible for the "memory management" of the program "runtime"
candidate : The memory structure of the JVM often refers to the "runtime data area" defined by the JVM
candidate : Simply put, it is divided into 5 blocks: method area, heap, program counter, virtual machine stack, and local method stack
candidate : It should be noted that this is the partition concept of the JVM "specification". When it comes to the specific implementation, different vendors' implementations may be different.
Interviewer : Well, by the way, let’s talk about the content of each area on your map.
candidate : Okay, then I will start with the "program counter".
candidate : Java is a multi-threaded language. We know that if the number of threads is greater than the number of CPUs, there is a possibility of "thread switching". Switching means "interruption" and "recovery", so a region is naturally needed To save the "execution information of the current thread"
candidate : Therefore, the program counter is used to record the address of the bytecode executed by each thread (branch, loop, jump, exception, thread recovery, etc. all depend on the counter)
Interviewer : Okay, I understand.
candidate : Then I will talk about the "virtual machine stack"
candidate : When each thread is created, a "virtual machine stack" is created, and a "stack frame" is created for each method call. Each "stack frame" will contain several pieces of content: local variable table, operand stack, dynamic connection and return address
candidate : After understanding the composition of the "virtual machine stack", it is not difficult to guess its role: it saves the method of local variables, calculation of some variables, and participates in the call and return of the method.
Interviewer : ok, got it
candidate "local method stack" below.
candidate : The function of the local method stack is similar to that of the virtual machine stack. The virtual machine stack is used to manage the calls of Java functions, and the local method stack is used to manage the calls of local methods. The "native method" here refers to the "non-Java method". Generally, the native method is implemented in C language.
Interviewer : Well...
candidate : Well, finished talking about the "local method stack", "virtual machine stack" and "program counter", oh, there are also "method area" and "heap" below
candidate : Then let me talk about the "method area" first
candidate : I mentioned earlier that the "partition" of the runtime data area is the "standard" of the JVM. The specific implementation may be different for different virtual machine vendors.
candidate : So the "method area" is only part of the specification in the JVM.
candidate : In the HotSpot virtual machine, the term "permanent generation" is often mentioned. The HotSpot virtual machine implemented the "method area" with the "permanent generation" "before JDK8", and many other vendors' virtual machines actually do not have the concept of "permanent generation".
candidate : We will use the HotSpot virtual machine to illustrate the following content.
candidate : In JDK8, "meta space" has been used instead of "permanent generation" as the implementation of "method area"
Interviewer : Well...
candidate : The method area is mainly used to store the "class related information" that has been loaded by the virtual machine: including class information and constant pool
candidate : The class information includes the version, fields, methods, interfaces, and parent classes of the class.
candidate : The constant pool can be divided into "static constant pool" and "runtime constant pool"
candidate : The static constant pool mainly stores information such as "literal" and "symbol reference". The static constant pool also includes what we call the "string constant pool".
candidate : "Runtime constant pool" stores information such as "direct references" generated during "class loading".
Interviewer : Well...
candidate : It is also worth noting that from the perspective of "logical partitioning" the "constant pool" belongs to the "method area"
candidate : But since "JDK7", the "runtime constant pool" and "static constant pool" have been transferred to the "heap" memory for storage (for the "physical partition", the "runtime constant pool" "And "static constant pool" belong to the heap)
Interviewer : Well, this is a lot of information
Interviewer : I would like to ask, you said that from "JDK8", the implementation of "method area" has been changed from "permanent generation" to "metaspace". What's the difference?
candidate : The main difference is that the "metaspace" storage is not in the virtual machine, but uses local memory. The JVM will no longer have memory overflow in the method area. In the past, the "permanent generation" often runs because of insufficient memory. OOM exception occurred.
candidate : According to the JDK8 version, in summary, it is equivalent to: "class information" is stored in the "meta space" (some people call the "class information" block "class information constant pool", which is mainly called Different, as long as the meaning is in place)
candidate : The "constant pool" starts with JDK7, and it is in the "heap" from the point of view of "physical storage". There is no change.
Interviewer : Well, I understand
Interviewer : talk about the "heap" area
candidate : Well, the "heap" is an area shared by threads, and almost all the memory allocated for class instances and arrays comes from it
candidate : "Heap" is divided into "new generation" and "old generation", "new generation" is further divided into Eden and Survivor areas, and finally Survivor is composed of From Survivor and To Survivor
candidate : not many BBs, let me draw pictures
candidate : Separate "heap memory" into several areas, mainly related to "memory collection" (garbage collection mechanism)
16181d819f1777 Interviewer the garbage collection next time. There will be a lot of things in this extension.
Interviewer : Would you like to talk about the difference between the JVM memory structure and the Java memory model?
candidate : They have nothing to do with each other. In fact, after the two interviews, you should feel it
candidate : The Java memory model is related to "concurrency". It is a specification proposed to shield the underlying details. It is hoped that the upper layer (at the Java level) will have the same effect on different platforms when operating memory.
candidate : Java memory structure (also known as runtime data area), which describes how the "logical structure" of each partition is when our class file is loaded into the virtual machine, and what role each partition plays .
Interviewer : I got it
summarizes today : JVM memory structure composition (JVM memory structure is also called "runtime data area". There are five main components: virtual machine stack, local method stack, program counter, method area and heap. The method area and heap It is shared by threads. The virtual machine stack, local method stack, and program counter are thread-isolated)
Welcome to follow my WeChat public [16181d819f181d Java3y ] to talk about Java interviews. The online interviewer series is being updated continuously!
[Online Interviewer-Mobile] The series updated twice a week!
[Online Interviewer-Computer] The series updated twice a week!
original is not easy! ! Seek three links! !
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。