During the interview , About Java Memory model （ Focus on ：Java The concurrency principle of ） and JVM The memory model of is not the same problem , So I summarize the relevant knowledge points after querying the data , If there's something wrong , Welcome to point out , We can also discuss .
Computer based memory design structure ,CPU In the process of development , Running faster and faster , The reading and writing speed of memory can't keep up with CPU Running speed of , Therefore, it is proposed that “ cache ” The concept of .CPU In the process of reading and writing , You can write data to the cache first , The cache then flushes the data into main memory . therefore , In the case of multithreading “ Cache inconsistency ”, That is, multiple threads may not cache the same data consistently .
Data based security , Need to satisfy atomicity 、 visibility 、 Orderliness
More specifically about CPU And memory between the love and hate entanglement, at present I am not in-depth , Interested students can learn about it by themselves
Java Memory model (JMM)
·· Java Memory model is a kind of memory model specification , Shielding the access differences of various hardware and operating systems , To ensure the Java It is a mechanism and specification that program can access memory in various platforms to ensure the consistent effect
First of all, three concepts , Basically, the memory model is designed to solve these three problems
Atomicity ： In one operation CPU It can't be suspended 、 interrupt , Or the execution is complete , Or not
visibility ： When multiple threads access the same variable , After a thread modifies a variable , Other threads can see the modified value immediately
Orderliness ： The program executes the code in order
summary ：JMM It's a norm , There is a problem of inconsistency between memory and cache data when it is used to deal with multithreaded shared memory
Java How to implement the memory model
Java Provides a series of concurrency related keywords , such as ：
In fact, these are Java The memory model is encapsulated in the bottom layer and provided to the programmer to use keywords after implementation
The focus of this article is to understand JMM, So I won't introduce the related keywords in detail ！！！！
synchronized Used to guarantee Atomicity
volatile For visibility
volatile and synchronized Can achieve visibility
JVM Memory model
Java Virtual machines divide managed memory into five areas ：
1. Program counter
Program counter is a very small memory space , It's thread private , It can be regarded as the line number indicator of the current thread .
Purpose ：CPU When performing multithreaded tasks , In order to ensure that the thread can switch to the correct execution position , Each thread has a separate program counter , Program counters between different threads do not affect each other , Independent storage .
Be careful ： If the thread is executing a java Method , So the counter records the address of the virtual machine bytecode instruction . If native【 Bottom method 】, Then the counter is empty . This memory area is the only one not included in the virtual machine specification OutOfMemoryError Region
2.Java Stack （ Virtual machine stack ）
Concept ： The stack describes Java Memory model for method execution , Each method creates its own memory stack to store local variables and other related information when it is executed , The way to store them is to store them one by one , So here's a stack in and out process .
When the method execution is complete , The memory stack is destroyed , So the variables defined in the method are placed in the memory stack .
3. Pile up
Concept ： The pile is Java The largest block memory area in the virtual machine , Because the objects stored in the heap are shared by threads , So multithreading also requires synchronization . Most of the object instances and arrays allocate memory on the heap , It's not always like this , The reason lies in JIT The maturity of compiler technology , This statement is not so absolute .
Be careful ： Why there are stacks and heaps ：
When a method executes , Each method establishes its own memory stack to store the variables defined in the method , Destroy at the end of the method , But when you create an object in a program , This object will be saved in the heap （ It can also be called ： Run time data area , The heap is part of it ）, It is convenient and can be used repeatedly , This is because the cost of creating objects is usually quite high . Let's make it this way , Then objects in heap memory will not be destroyed with the end of the method , When this object is referenced by a method, it may also be used by other reference variables , The object will not be destroyed . Only when an object does not have any reference variables to refer to it , The garbage collection mechanism of the system will recycle it at the right time （ Does it sound familiar , Special like GC）
4. Method area
Concept ： The concepts of method area and heap are similar , Is the memory shared by all threads , To distinguish it from the heap , Also known as “ Non heap ”
Purpose ： Used to store class information loaded by virtual machine 、 Constant 、 Static variables
5. Native Method Stack
Concept ： The native method stack is very similar to the virtual machine stack , The difference is that the virtual machine stack performs Java Method ( That's bytecode ) service , The local method stack is used by the virtual machine native Method service , Maybe it's called at the bottom c perhaps c++, We turn on jdk The installation directory can be seen and has many uses c Documents prepared , It could be native Method c Code .
GC：java GC Generally speaking java Garbage collection mechanism , The mechanism is java And C/C++ One of the main differences
Purpose ： In order to solve the problem of memory leak （ Try to solve , After all, what should be leaked will be leaked ）
GC I won't give a detailed introduction here , After all, I am also in the process of learning ,GC It's something you can talk about for a long time , I haven't summed it up yet .