Interviewer: Today let’s talk about the memory structure of the JVM.
Candidate: Well, ok
Candidate: As mentioned in previous interviews, class files are loaded by the classloader into the JVM, and the JVM takes care of the “memory management” of the program “runtime.”
Candidate: The memory structure of the JVM is often referred to as the “runtime data region” defined by the JVM
Candidates: In a nutshell, they are divided into five blocks: method area, heap, program counter, virtual machine stack, and local method stack
Candidates: It is important to note that this is the concept of partitioning as a “specification” for the JVM, and vendor implementations may differ when it comes to implementation.
Interviewer: Well, by the way, tell me something about each area on your chart.
Candidate: Ok, I’ll start with program counters.
Candidate: Java is a multithreaded language, and we know that if the number of threads is greater than the number of cpus, there is a high probability of “thread switching”. Switching means “interruption” and “recovery”, so naturally there needs to be an area for “current thread execution information”.
Candidate: So, a program counter is the address used to record the bytecode executed by each thread (branches, loops, jumps, exceptions, thread recovery, and so on all depend on counters)
Interviewer: OK, GOT it.
Candidate: Let me talk about the virtual stack
Candidate: Each thread creates a “virtual stack” when it is created, and a “stack frame” is created for each method call. Each “stack frame” contains several pieces of content: a local variable table, an operand stack, a dynamic link, and a return address
Candidate: Once you know what a “virtual stack” is, it’s not hard to guess what it does: it holds local variables for methods, evaluates partial variables, and participates in method calls and returns.
Interviewer: OK, I see
Candidate: Let’s talk about the “local method stack.
Candidate: The local method stack is similar to the virtual machine stack, which is used to manage calls to Java functions, and the local method stack, which is used to manage calls to local methods. By “native methods” I mean “non-Java methods”. Native methods are generally implemented in THE C language.
Interviewer: HMM…
Candidates: Well, having said “local method stack,” “virtual machine stack,” and “program counter,” there’s also “method area” and “heap.”
Candidate: Let me start with the method area
Candidate: As mentioned earlier, this “partition” is the “specification” of the JVM. The implementation may vary from virtual machine vendor to virtual machine vendor
Candidate: So the “method area” is just part of the specification in the JVM.
Candidate: In HotSpot VIRTUAL machines, the term “persistent generation” is often mentioned. The HotSpot VIRTUAL machine implemented the “method area” with “persistent generation” before “JDK8”, whereas many other virtual machines did not have the concept of “persistent generation”.
Candidate: Let’s use the HotSpot VIRTUAL machine to illustrate the rest of this.
Candidate: In JDK8, “meta-space” has been used instead of “permanent generation” as an implementation of “method areas”
Interviewer: HMM…
Candidate: The method area is used to store class-related information (including class information and constant pool) that has been loaded by VMS
Candidate: Class information includes the version, fields, methods, interfaces, and parent classes of the class.
Candidate: Constant pool can also be “static constant pool” and “runtime constant pool”
Candidates: Static constant pools mainly store literals and symbolic references. Static constant pools also include what we call string constant pools.
Candidate: The runtime constant pool stores information such as direct references generated during class loading.
Interviewer: HMM…
Candidate: It is also worth noting that the constant pool belongs to the method area from the perspective of the logical partition
Candidate: But since “JDK7”, “runtime constant pool” and “static constant pool” have been moved to “heap” memory for storage (for “physical partition” “run time constant pool” and “static constant pool” are heap)
Interviewer: Well, that’s a bit too much information
Interviewer: I would like to ask, you said that “JDK8” has changed the implementation of “method area” from “permanent generation” to “meta space”, what is the difference?
Candidates: The main difference is that the “meta-space” storage is not in the VIRTUAL machine, but instead uses local memory. The JVM will no longer run out of memory in the method area.
Candidate: according to JDK8 version, summed up actually equivalent: “class information” is stored in the “meta space” (also some people call “class information” piece called “class information constant pool”, mainly is different, meaning is in place good)
Candidate: Whereas “constant pool” starts with JDK7 and is “in the heap” from a “physical storage” perspective, this is unchanged.
Interviewer: Yes, I understand
Interviewer: Finally, let’s talk about the “heap” area
Candidates: Well, the “heap” is the area shared by threads, where almost all instances of classes and arrays are allocated memory
Candidates: “heap” is divided into “New generation” and “old generation”, “new generation” is further divided into Eden and Survivor region, and Survivor consists of From Survivor and To Survivor
Candidate: not much BB, I also draw a picture
Candidate: Separate areas of heap memory, mainly related to memory collection (garbage collection)
Interviewer: Let’s save the garbage collection for another time. That leads to a lot of things
Interviewer: Why don’t you tell me the difference between the JVM memory structure and the Java memory model?
Candidate: There is no direct connection between the two, in fact, after two interviews, you should have a feeling
Candidate: The Java memory model is about “concurrency,” a specification that was proposed to mask the low-level details, hoping that at the upper level (at the Java level) memory manipulation would have the same effect on different platforms
Candidate: Java memory structures (also known as runtime data areas), which describe the “logical structure” of the partitions and what each partition does when our class files are loaded into the virtual machine.
Interviewer: I see
Summary of the day: JVM memory structure composition (JVM memory structure also known as “runtime data region”. It consists of five parts: virtual machine stack, local method stack, program counter, method area and heap. The method area and heap are shared by threads. Virtual machine stacks, local method stacks, and program counters are thread-isolated.)
Welcome to follow my wechat official account [Java3y] to talk about Java interview, on line interview series continue to update!
Online Interviewer – Mobile seriesTwo continuous updates a week!
Line – to – line interviewers – computer – end seriesTwo continuous updates a week!
Original is not easy!! Three times!!