[TOC]
(1) Java memory area management
C/C++ each new operation needs to delete/free, while Java has automatic memory management, which is not prone to memory leakage or overflow, but does not mean that it does not occur. It is very important to understand how virtual machines use and manage memory, which is helpful for program optimization or troubleshooting.
The runtime areas are divided into:
- Thread private:
- Program counter:
Program Count Register
, thread private, no garbage collection - Virtual machine stack:
VM Stack
, thread private, no garbage collection - Local method stack:
Native Method Stack
, thread private, no garbage collection
- Program counter:
- Thread sharing:
- Methods area:
Method Area
In order toHotSpot
For example,JDK1.8
Post meta space replaces method area with garbage collection. - Pile:
Heap
The most important place for recycling.
- Methods area:
1.1 Program counter
The space is small, and the line number indicator (thread-specific, indicating where the current execution is and which bytecode needs to be executed next), branches, loops, jumps, exception handling, and thread recovery all depend on it. Thread private: Java multithreading is actually the thread in turn switch and allocate processor execution time implementation, a core at a specific point in time, will only execute the instructions of a thread. Thread switching requires saving and restoring the correct execution location (protection and recovery sites), so different threads require different program counters.
- perform
java
Method, the program counter records the address of the bytecode instruction being executed - perform
Native
Method, the program counter is empty
The only region that does not specify any OutOfMemory and does not have GC (garbage collection).
1.2 VM Stack
Threads are private and have the same life cycle as threads, mainly recording the in-memory model of the execution of Java methods on that thread. There are a lot of frames in the virtual machine stack. Note that the virtual machine stack corresponds to Java methods, not local methods. A Java method execution creates a stack frame, which stores:
- Local variable scale
- The operand stack
- Dynamic link
- Methods the export
When each method is called, a stack frame is placed on the virtual machine stack (pushed), and when the method is executed, the corresponding stack frame is ejected from the virtual machine stack (pushed).
Each thread has its own virtual stack, so it doesn’t get mixed up, which can cause confusion if it’s not thread independent.
We usually say that Java memory is divided into heap and stack, in fact, for the convenience of not too strict statement, they say the stack generally refers to the virtual machine stack, or the virtual machine stack in the local variable table.
The local variable scale generally stores the following data:
- Basic data types (
boolean
.byte
.char
.short
.int
.float
.long
.double
) - Object reference (the reference type, not necessarily the object itself, may be a reference pointer to the starting address of the object, or a handle representing the object, or a location associated with the object)
- ReturAddress (refers to the address of a bytecode instruction)
Local variable table memory size is determined during compilation and does not change during runtime. The space measure is called Slot (local variable space). 64-bit longs and doubles occupy two slots, and other data types occupy one Slot.
Exception:
- StackOverflowError: The stack depth requested by the thread is greater than the depth allowed by the virtual machine
- OutOfMemoryError: Insufficient memory
1.3 Local method stack
Similar to the virtual machine stack, corresponding to the Native method, the virtual machine specification allows different languages, use methods and data structures, some may merge the virtual machine stack and the local method stack. The exception is consistent with the VM stack:
- StackOverflowError: The stack depth requested by the thread is greater than the depth allowed by the virtual machine
- OutOfMemoryError: Insufficient memory
1.4 Java heap
The heap is the largest chunk of memory management, shared by threads.
The virtual machine specification states that all object instances and arrays are allocated on the heap. But not all objects are actually allocated on the heap, and this has to do with the development of JIT compilers and escape analysis techniques. According to? // TODO heap subdivision: new generation, old generation, subdivision includes Eden, From survivor, To survivor, etc.
There may also be thread-private areas in the heap that allocate buffers.
Physically discontinuous, but logically continuous.
Exception:
- OutOfMemoryError: Insufficient memory
1.5 method area
Named non-heap, but actually, like a heap, is a region shared by threads that stores the following information:
- Information about classes that have been loaded by VMS
- constant
- A static variable
- Just-in-time compiler compiled code
The method area is not equal to the permanent generation, instructing the Hotspot VIRTUAL machine to extend GC generation collection to the method area, i.e. implement the method area with the permanent generation, while other virtual machines are not necessarily fixed. JDK1.7 has removed string constants for persistent generations.
Method area garbage collection is not very effective, you can choose not to collect, the VIRTUAL machine can decide, of course, memory leakage may occur. Exception:
- OutOfMemoryError: Memory allocation is abnormal
1.5.1 Runtime constant pool
The runtime constant pool is part, but not all, of the method area. The Class file mainly contains:
- The version of the class
- field
- methods
- interface
- Constant pool, which stores literal and symbolic references generated by compilations. In addition to symbolic references describing Class files, there are also direct references. Is dynamic and can be generated at runtime, such as the string.intern () method.
Exception:
- OutOfMemoryError: Memory allocation is abnormal
(2) Direct memory
It is not a run-time data area of the virtual machine, nor is it specified by the specification, but is frequently used and may have OutofMemoryErrors: memory allocation exceptions. For example, NIO (1.4) is based on Channel and Buffer I/O. Native functions can be used to directly allocate out-of-heap memory, and DirectByteBuffer objects stored in the Java heap can be used as references to improve performance, without requiring both the Java heap and Native heap to copy data back and forth.
Direct memory is limited by physical memory, processor addressing space, and so on.
This article is related to JVM learning notes, organized from Zhou Zhiming teacher “deep understanding of Java virtual machine”, very admired, strongly recommended!
[Author profile] : Qin Huai, public number [Qin Huai Grocery store] author, the road of technology is not at that time, mountain high water long, even slow, chi and not stop. The world wants everything to be fast and faster, but I hope I can take every step, write every article, and look forward to communicating with you.
This article only represents their own (this rookie) learning accumulation record, or learning notes, if there is infringement, please contact the author to verify deletion. No one is perfect, the article is the same, the writing style is immature, I am not talented, do not spray, if there is a mistake, also hope to point out, grateful ~