First, memory management
TwoThread Exclusive ZoneProgram Counter Register)
A program counter is a small amount of memory space, which can be seen as the line number indicator of the bytecode executed by the current thread. In the virtual machine conceptual model, the bytecode interpreter works by changing the value of this counter to select the next byte-code instruction to execute, and the basic functions such as branching, looping, jumping, exception handling, thread recovery, and so on, need to rely on this counter to complete.
If the thread is executing a Java method, this counter records the address of the executing virtual machine bytecode instruction, or null (Undefined) If the native method is being executed. This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions
Threethe thread exclusive zoneJava VM Stack (Java Virtual machine Stacks)
Fourthe thread exclusive zoneLocal methods Stack (Native method Stacks)
The local method stack is very similar to the virtual machine stack, except that the virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine, and the local method stack serves the native method used by the virtual machine.
Because the virtual machine specification does not enforce the language, usage, and data structure of the local method stack, some virtual machines (such as sun hotspot virtual machines) directly combine the local method stack with the virtual machine stack.
The local method stack also throws Stackoverflowerror and outofmemoryerror exceptions.
Java heap of thread sharing area (Java heap)
The Java heap is the largest piece of memory managed by a Java virtual machine and is created at the time the virtual machine is started, with the sole purpose of storing object instances: All object instances and arrays are allocated on the heap (but with the development of the JIT compiler and the escape analysis technology gradually mature, The allocation of all objects on the heap is gradually not so absolute.
Java heap is the main area of garbage collector management, and now the collector basically uses the generational collection algorithm , so the Java heap can be subdivided into: The new generation and the old age ; From survivor Space , tosurvivor space , etc.
According to the Java Virtual Machine specification, the Java heap can be in a physically discontinuous memory space as long as it is logically contiguous .
A OutOfMemoryError exception is thrown if there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.
Vi. method area of thread sharing area
Used to store data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine.
The so-called method area is the term for the permanent generation (Permanent Generation) , simply because the hotspot virtual machine extends the GC generation collection to the method area, or uses the permanent generation to implement the method area. For other virtual machines there is no permanent generation of claims.
6.1 Running a constant-rate pool (runtime Constant)
A run-time constant pool is part of a method area that holds the various literal and symbolic references generated by the compiler, and typically contains direct references that are translated. This section is stored in the run-time pool of the class load backward into the method area.
The OutOfMemoryError exception is thrown when the constant pool is no longer able to request memory.
6.2 Cases
Public class Testjconsole { publicstaticvoid main (string[] args) { = "abc" ; New String ("abc"); = = str2) ; = = Str2.intern ());} }
Results:
- For example,string str1 = "abc" str1 points to a constant pool, while string str2 = new String ("abc") , str2 points to the heap memory object, The address is different so str1 = = str2 The result is false, but Str2.intern () moves the string value from the heap memory to the constant pool (if the constant pool exists, returns the address of the value). This way both str2 and STR1 are the ABC that points to the constant pool.
Vii. Direct Memory
Direct memory is not part of the data area when the virtual machine is running, nor is it an area of memory defined in the VM specification, but it can also cause a outofmemoryerror exception to occur.
For example, the newly added NiO (new Input/output) class in JDK1.4 introduces a method of I/O based on channel and buffer, which he can use to directly allocate out-of-heap memory using the native library. It then operates as a reference to this memory through a Directbytebuffer object stored in the Java heap. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and native heap.
Native Direct memory is not limited by the size of the Java heap, but is limited by the size of the native total memory (including RAM and swap or paging files) and the processor addressing space. A outofmemoryerror exception occurred while dynamically expanding.
(iii) Java Virtual machine memory management and thread exclusive and thread sharing areas