Java memory management and memory overflow exceptions, java memory management Overflow
Speaking of memory management, I would like to first compare the differences between java and C ++:
- In C and C ++, the programmer is responsible for memory management. That is to say, the programmer must complete the heavy coding work and always take into account the maintenance of the system memory.
- In java, the programmer does not need to consider the control and maintenance of memory, but is handed over to the JVM for automatic management, so that the memory leakage and overflow problems are not prone. However, once Memory leakage and overflow problems occur, it is difficult to find the error if you do not understand the JVM memory management mechanism.
1. JVM runtime data Zone
When running a java program, the JVM divides the memory it manages into several different regions, which have their own purposes and the creation and destruction time. Generally, it includes the following data partitions:
The orange area is private to each thread, that is, each thread has its own copy, while the green area is shared by each thread.
(See: http://blog.csdn.net/seu_calvin/article/details/51404589 for details)
2. Create a java object
When the JVM scans the new Keyword, it first checks whether the parameter of this instruction can locate the symbolic reference of a class in the constant pool, check whether the symbolic reference of this class indicates that the class has been loaded, parsed, and initialized. If no, the corresponding class loading process must be executed first.
After the class loading check is passed, the JVM needs to allocate memory for the new object, that is, to divide a fixed-size memory from the java heap. There are two common partitioning methods:Pointer collision(Requires the heap memory to be absolutely regular ),Idle list(The heap memory is not regular ).
JVM needs to initialize the allocated memory space to a zero value (excluding the object header), which ensures that the instance field of the object can be used directly without the initial value in java code, that is to say, the program can access the zero value corresponding to the Data Type of these fields.
Execute the <init> method to initialize the object according to the programmer's wishes.
3. Access and location of Objects
After the object is created, we also hope to quickly access these objects. This requires reference data on the JVM stack to find specific objects in the stack, currently, the most frequently used access method is"Handle MethodAndDirect pointer.
- If you use the handle method for access, you need to divide some memory in the heap as the handle pool. The reference variable stores the handle address of the object, the handle contains the specific address information of the object instance data and type data.
- If direct pointer access is used, the object address is stored in the reference variable, but you need to consider how to place information about the type of data.
4. memory overflow exception
In addition to the PC register, memory overflow may occur in other memory areas of the Data zone during JVM running. The PC register is the only region in which the JVM specification does not specify any OutOfMemoryError (OOM) conditions.
The heap in java is used to store object instances. If you constantly create objects and ensure that there is a reachable path between GC Roots and objects to avoid GC collection, A heap overflow exception occurs when the number of objects reaches the maximum heap capacity limit.
- Stack Overflow (including JVM stack and local method stack)
1. If the stack depth requested by the thread is greater than the maximum depth allowed by JVM, an StackOverflowError exception is thrown;
2. If the JVM cannot apply for sufficient memory space when extending the stack, an OutOfMemoryError error will be thrown.
- In addition, there are methods overflow, constant pool overflow, local memory overflow, and so on.