Java And C++ There is a gap between the dynamic allocation of memory and garbage collection technology “ High wall ”, People outside the wall want to go in , The people in the wall came up with
Run time data area
- Data area shared by all threads ： Method area 、 Pile up
- Thread isolated data area ： Virtual machine stack 、 Native Method Stack 、 Program counter
Program counter (Program Counter Register)
- Thread private
- A small memory space
- The bytecode interpreter selects the next bytecode instruction to execute by changing the value of this counter , Branch 、 loop 、 Jump 、 exception handling 、 Thread recovery etc Basic functions all need to rely on this counter
- Java Multithreading of virtual machines ： Through the way that threads switch in turn and allocate processor execution time , Any definite moment , A processor can only execute instructions in one thread . therefore , In order to recover to the correct execution position after thread switching , Each thread needs to have a separate program counter , Counters between threads do not affect each other , Independent storage .
- This memory area is the only one in Java Nothing is specified in the virtual machine specification OutOfMemoryError Area of situation
Java Virtual machine stack
- Thread private
- Each method creates a stack frame as it executes （stack frame） Used to store local variables （ Stack memory ）、 The stack of operands 、 Dynamic links , Method exit information .
- The process of each method from call to execution , Corresponding to a stack frame in the virtual machine stack stack to stack out of the process
- The local variometer holds various basic data types known at compile time （boolean、byte、char、short、int、float、long、double）、 Object reference （reference type , It could be a pointer to the starting address of the object , It could also be a handle to a delegate object ） and returnAddress type （ Points to the address of a bytecode instruction ）
- 64 Bit long and double Occupy 2 Each local variable space . The size of the local variable table does not change during the run of the method
- There are two kinds of anomalies in this area ： The thread request stack depth is greater than the virtual machine allows , Throw out StackOverflowError abnormal ; If the virtual machine stack can be expanded dynamically , Can't request enough memory when expanding , It will be thrown out. OutOfMemoryError abnormal
- summary ： Stack frame ：
- Local variable table
- Various basic data types
- Object reference
- returnAddress type
- The stack of operands
- Dynamic links
- Methods the export etc
- Local variable table
Native Method Stack （Native Method Stack）
- Thread private
- The difference from the virtual machine stack ： The virtual machine stack executes for the virtual machine Java Method （ That's bytecode ） service , The local method stack is used by the virtual machine Native Method service
- Sun HotSpot Virtual machine combines local method stack with virtual machine stack
- Same as virtual machine stack , Two kinds of exceptions will also be thrown
Java Pile up （Java Heap）
- Most applications , The pile is java The largest chunk of memory managed by a virtual machine .
- A memory area shared by all threads
- Created when the virtual machine starts
- Store object instance , Almost all object instances allocate memory here .Java The virtual machine specification describes ： All object instances and arrays are allocated on the heap , But as the JIT The development of compiler and the technology of escape analysis , On the stack 、 Scalar substitution  Optimization techniques will lead to subtle changes , All the objects are distributed on the heap, and it's becoming less and less “ absolute ”.
- The main area of garbage collector Management , Also known as GC Pile up （Garbage Collected Heap）
- From a memory recovery perspective , Now collectors basically use generation by generation collection algorithm , therefore Java The heap can also be subdivided ： The new generation 、 Old age ; There are more detailed Eden Space 、From Survivor Space 、To Survivor Space .
- From the perspective of memory allocation , Thread shared heap can be divided into multiple thread private allocation buffer （Thread Local Allocation Buffer,TLAB）, The purpose of partitioning is to better reclaim memory , Or allocate memory faster .
- The heap can be in a physically discontinuous memory space . It can realize the fixed size of the city , It can also be extensible （ adopt -Xmx and -Xms control ）.
- There is no memory in the heap to complete instance allocation , And the heap cannot be extended , It will be thrown out. OutOfMemoryError abnormal
Method area （Method Area）
- An area shared by all threads
- Store class information loaded by virtual machine 、 Constant 、 static state b Variable 、 Just-in-time compiler compiled code and so on
- 1.7 The method area is described as a logical part of the heap , But it's not heaps （non-heap）
- stay hotspot For developers on virtual machines , The method area is also known as the permanent generation , But the two are not equivalent , Because hotspot Our design team put GC The generational collection is extended to the method area . Other virtual machines （ Such as BEA JRockit、IBM J9 etc. ） There is no concept of permanent generation
- Method area GC Recycling may not be realized
- Method area （ The concept of permanent generation lies in 1.8 Be cancelled , Instead of ）
Runtime constant pool （Runtime Constant Pool）
- Is part of the method area
- Store all kinds of literal and symbol references generated by the compiler , This part of the content enters the runtime constant pool of the method area after the class is loaded
- Runtime constant pool relative to class One of the important characteristics of the file constant pool is its dynamic nature , Constants don't have to be generated only at compile time , It is also possible to pool new constants during runtime , such as string Of intern() Method
- If the constant pool cannot apply for memory, it will throw OutOfMemoryError abnormal
Direct memory （Direct Memory）
- Not part of the virtual machine runtime data area
- 1.4 New time to join NIO（New input/output） class , Introduce channel based channel With the buffer buffer Of I/O The way , have access to Native Function library directly allocates out of heap memory , And then through a Java Heap DirectByteBuffer Object operates as a reference to this memory
- Native direct memory allocation is not limited by heap size , But it is limited by the size of the local memory and the addressing space of the processor . When configuring virtual machine parameters , According to the actual memory settings -Xmx Isoparametric information , When ignoring direct memory , Make the sum of each area greater than the physical memory limit , Cause dynamic expansion to appear OutOfMemoryError abnormal