Home  >  Article  >  Java  >  Detailed introduction to Java memory management and memory overflow exceptions

Detailed introduction to Java memory management and memory overflow exceptions

黄舟
黄舟Original
2017-10-19 10:20:431670browse

The following editor will bring you a brief discussion of java memory management and memory overflow exceptions. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

Speaking of memory management, the author would like to first compare the differences between Java, C and C++:

In C and C++, memory management is the responsibility of programmers, which means that programmers have to complete heavy code writing work and always consider the maintenance of system memory

In java, Programmers do not need to consider memory control and maintenance, but let the JVM automatically manage it, so that memory leaks and overflow problems are less likely to occur. However, once problems with memory leaks and overflows occur, it is difficult to find the error without understanding the JVM's memory management mechanism.

1. JVM runtime data area

When the JVM runs a java program, it will divide the memory it manages into several different Areas, these areas not only have their own purposes, but also have time for creation and destruction. Generally speaking, it contains the following runtime data areas:

The orange area is private to each thread, that is, each thread will have its own copy, and the green area The area is shared by each thread.

2.Creation of java objects

Class loading check

When the JVM scans the new keyword, it will first check whether the parameters of this instruction can locate a symbolic reference of a class in the constant pool, and check whether the class represented by the symbolic reference of this class has been loaded. Parsed and initialized. If not, the corresponding class loading process must be performed first.

Memory allocation

When the class loading check passes, the JVM needs to allocate memory for the new object, that is, divide a memory of a certain size from the java heap. There are two commonly used division methods: Pointer collision (the heap memory is required to be absolutely regular), free list (the heap memory is not regular).

Memory initialization

The JVM needs to initialize all allocated memory spaces to zero values ​​(excluding object headers), which ensures that the instance fields of the object are in java The code can be used directly without assigning initial values, which means that the program can access the zero value corresponding to the data type of these fields.

Object initialization

Execute the method to initialize the object according to the programmer's wishes.

3. Object access positioning

After the objects are created, we also hope to access these objects quickly, which requires JVM The reference data on the stack is used to find the specific object in the heap. Currently, the most commonly used access methods are "handle method" and "direct pointer".

If you use the handle method to access, you need to divide a part of the memory in the heap as a handle pool. The reference variable stores the handle address of the object, and the handle contains the specific details of the object instance data and type data. Address information.

If you use direct pointer access, what is stored in the reference variable is directly the object address, but you need to consider how to place the relevant information of the type data.

4. Memory overflow exception

In addition to the PC register, the JVM runtime data area has other Memory overflow exceptions may occur in any memory area. The PC register is the only area that does not specify any OutOfMemoryError (OOM) conditions in the JVM specification.

Heap overflow

The heap in java is used to store object instances. If objects are continuously created, and there is a reachable path between GC Roots and the object to avoid GC recycling processing, then a heap overflow exception will occur after 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 the JVM , a StackOverflowError exception will be thrown;

2. If the JVM cannot apply for enough memory space when expanding the stack, an OutOfMemoryError exception will be thrown.

In addition, there are method area overflows, constant pool overflows, native memory overflows, etc.

The above is the detailed content of Detailed introduction to Java memory management and memory overflow exceptions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn