This article introduces an introduction to the Java Memory Data model, which is great for our deep understanding of the workings of the JVM virtual machine and the partitioning of Java memory, so that, to make our understanding more profound, we will add the image-assisted approach to understanding.
Directory of this blog post:
One: Introduction to the Java Memory data model
Two: Thread private memory
Three: Program counter
Quad: Java Virtual machine stack
V: Local Method stack
VI: Java heap
Seven: Method area
Eight: Run a constant-rate pool
IX: Summary
One: Introduction to the Java Memory data model
Java programs run in memory to open up different spaces to manage unused memory areas, each region has its own function, create and destroy time, and some areas will be created as the virtual machine starts, and some areas will be destroyed as the user thread is created. According to space, divided into the following different space, then we will follow the different areas of learning, learning its different areas of function and usage
Figure 1-1: Java Memory data Model
Two: Thread private Memory
Let's take a look at the multi-threaded operation principle: In fact, the CPU runs at the same time at a specific point in the runtime only one thread (the case of a single core), but it is very fast switching speed, let us feel it is in the multi-threaded operation, essentially it is still a single run. So this will inevitably lead to a problem: how the CPU switching thread ensures that its own operation is not affected by other threads, and that each thread is independent from external intrusion, which creates the concept of thread-private memory, mainly to maintain thread security, stability, and efficient operation. About this is also very good understanding, such as we go to a fast restaurant to eat, we ordered the dishes, will produce a menu ticket, this small ticket has a number, is the "private memory" of each thread, the waiter and then different customers to serve the time there is a distinction, so you can successfully find different customers (switch different threads) In Java threads, program counters, virtual machine stacks, and local method stacks belong to thread-private.
Note: Thread-Private does not have multi-threaded concurrent resource contention because the memory it enjoys does not affect each other and there is no concurrency problem
In contrast to thread-private, on-wire public memory, this area has a specific salutation in the JVM virtual machine called: main Memory
Three: program counter:
the darker red part of the mark on the graph, which refers to the line number indication of the bytecode (. Class) file executed by the program during the run, is also called the line number indicator, and the basic functions such as branching, looping, and exception handling of the program flow need to be guided to complete. It occupies a small amount of memory, its operation principle is: by changing the value of the counter to select the next code to execute the bytecode command, and this value specifically refers to the virtual machine in the Java method of the bytecode instruction address, but if the native method is executed, then its counter value is empty.
Note :
1: It is the only area in Java that does not have a memory overflow (out of memories) situation. Can you think about why this is? It is the LED that runs the thread, and if it does not, the program will not run.
2: It belongs to the thread-private: There is a program counter inside each thread, in order to ensure that each thread will work before and after switching.
Quad:java Virtual machine stack:
The virtual machine stack is primarily a function when the Java method runs, each thread creates a stack frame at the entrance to a method, creating a stack frame to store information such as local variables, operand stacks, dynamic links, method exits, and so on. According to this principle, a method will correspond to a process from the stack to the stack from the call to the full execution.
Here we explain the local variables, which means variables defined within the Java method, corresponding to the 8 basic data types in Java, byte, int, float, Boolean, char, short, long, reference: (Long and double occupy 2 space, the remaining 1) local variable table We need to note that it has been fully allocated in the compilation period, so that the stack frame in the use of local variables, it occupies the memory is already confirmed, do not need redistribution, which reduces the workload of the stack frame to a certain extent.
Attention:
1: StackOverflow exception is thrown when thread requests a stack frame that is deeper than the virtual machine allows
2: What we usually call the stack, where the stack is referring to the stack frame here
3: It is also thread-private and is the same as the thread's life cycle.
Figure 2-1: Virtual machine stack
Five: Local method Stack:
The local method stack, as the name implies it is the local method native execution of the stack frame, in the JDK source code we can see a lot of named native method, native method is written in a large part of C + +, However, it does not make any specific requirements for the data structure, language, etc. used by native, which is completely implemented by the Java virtual machine.
Attention:
1: This area will throw an oom or SOF (stackoverflowerror) exception
2: Likewise it belongs to the local thread private
Six: java heap:
The Java heap is primarily where objects are stored, and it is the largest chunk of the Java Memory Management area. When we new an object in the program or create a new array, the object is stored in this area. It is one of the most important public areas of Java, relative to the thread private, which belongs to the thread-public. Basically all objects are stored in this area, but they are not absolute (and as the JIT technology matures, this happens slightly). The heap is the main place to store objects, and it will produce another problem is the GC, when garbage recycling, the virtual machine does not open up new space, still in this area, and this area is also called "GC Heap", and then carefully divided into the next generation, the old age, but also divided into the Eden space, from Survivor space, to Survior area. Why is it so carefully divided? In fact, or mainly in order to more efficient recovery, the more detailed division of the area, then the garbage collector when collected as long as the corresponding place to directly reclaim the line, do not add additional judgment logic.
Note the point:
1: The heap is logically continuous uninterrupted memory space, but can be physically discontinuous memory space
2: An oom is thrown in the heap if there is no memory-complete instance assignment
The 3:java heap is thread-public and all threads share this area
Figure 3-1 Java heap Memory
Seven: Method area
The method area is primarily used to store data such as class information, constants, static variables, and compiler-compiled code that have been loaded by the virtual machine, such as the constant constants we define in our code are stored in this region. In the Java Virtual Machine specification, it belongs to the logical part of the heap. At the same time in this area, it will also have garbage collector work, this area is called "permanent generation", the reason is called permanent generation, because it has a longer life cycle than the new generation and the old age, but not in this region it will be all right, the permanent generation will still have garbage collection situation, but relatively less
Note the point :
1: This area is thread-public, thread-class information, constants, static variables, compiled code are stored in this area
2: This area throws an Oom exception that occurs when the method area is unable to allocate memory
Figure: 4-1 Method Area
Eight: Run a constant-rate pool:
The run-time pool is mostly part of the method area, except that the class file has a description of the version, field, method interface, and so on, and there is a constant pool that stores the various literal and symbolic references generated during the compilation period. This section will be stored in the run-time pool of the class load backward into the method area.
Note : Oom exceptions also occur in this area
IX: Summary
This blog post is mainly to the Java Memory Data Model Area Division has made a simple introduction, not in-depth detailed introduction. However, we have a perceptual understanding on the whole, understand the division of the Java Memory area, and the benefits of such partitioning, as well as the design to the thread part, understanding the concurrency problem. This part belongs to the bottom of the virtual machine, which belongs to the advanced level. I believe after reading this article, I hope to answer the following several questions. At the same time, these things are also well-known in our understanding of the bottom, the interview is often mentioned in the things
What areas are the 1:java memory models divided into?
2: Name the area that belongs to the thread's private part and the thread's public part?
3: We all know that memory is divided by "generation", so what are the generations of memory in the Java heap? In what area?
4: Why divide the Java memory so much, divide so much is not too troublesome? What good is it?
I hope to think about the above problems, when learning to think more and think deeply. This is the end of this blog post, the next article, the understanding of the Java memory model can help us to deal with concurrency problems, this is part of Java EE in the high-level Advanced Level essential foundation, here to leave a record, while sharing, so that you can also clearly understand the Java memory model and remind yourself to understand deeply.
Main references:
"Deep JVM virtual Machine"
Java Memory Data Model