Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to check and solve the memory overflow problem of java virtual machine
How to check and solve the memory overflow problem of java virtual machine
First, the jvm memory area

1, program counter

A small memory space as a line number indicator of bytecode executed by the current thread.

2. java stack

Like program counters, java stack is private to threads, and its life cycle is the same as that of threads. Generally, the basic data type, object reference (reference pointer to the starting address of the object or handle representing the object) and return address type (address to bytecode instruction) are stored.

There are two types of exceptions in the stack area: if the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StrackOverflowError exception will be thrown; If the virtual machine stack can be dynamically expanded (most virtual machines can be dynamically expanded), when enough memory cannot be applied during the expansion, an OutOfMemoryError exception will be thrown.

3. Local method stack

Similar to the virtual machine stack, the difference is that the virtual machine stack performs java method services for the virtual machine, while the local method stack serves the native methods used by the virtual machine. Like the virtual machine stack, StackOverflowError and OutOfMemoryError exceptions may be thrown.

4. java heap

Java language (a computer language, especially for creating websites)

Heap is the largest memory area managed by jvm. JavaHeap is a memory area shared by all threads and is created when the virtual machine is started. Primary storage object instance. JavaHeap

It is the main field of garbage collector management, which can be subdivided into Cenozoic and old times. If there is no memory in the heap to complete the instance allocation and it can't be expanded, an OutOfMemoryError will be thrown.

Not normal.

5. Method area

Like javaHeap, it is a memory area shared by each thread, which is used to store class information, constants, static variables, code compiled by the compiler in time and other data that has been loaded by the virtual machine. When the method area can't meet the requirements

When the allocated requirements are saved, an OutOfMemoryError exception will be thrown. This method also contains a common runtime constant pool, which is used to store various literal quantities and symbolic references generated at compile time.

6. Direct memory

Direct memory is not a part of the runtime data area of the virtual machine, nor a memory area defined in the java virtual machine specification, but a memory area outside the jvm, which may also lead to an OutOfMemoryError exception.

Second, jvm parameters.

-Xss (stack space) stack space

-Xms, -Xmx (heap memory

Space) heap space: heap is the most familiar area, which jvm uses to store object instances. In a 32-bit system, the maximum capacity of the heap is 2G, and its size consists of -Xms and.

-Xmx, where -Xms is the minimum heap memory applied for when jvm starts, and it is 1/64 of physical memory by default, but less than 1G, and -Xmx is the maximum heap memory that jvm can apply for.

The default is 1/4 of physical memory, which is generally less than1g. By default, when the free heap memory is less than 40%, the jvm will maximize the heap size to the specified size -Xmx, which can be achieved by

-XX:minheafpreeratio to specify this ratio. When the free heap memory exceeds 70%, the JVM will adjust the heap size to the size specified by -Xms, which can be achieved by

-xx: maxheapatio to specify this ratio, but in order to avoid frequent resizing of HeapSize, the values of -Xms and -Xmx are usually set to be the same.

-XX:PermSize -XX:MaxPermSize: the persistent generation size of the method area: the method area is also the world * * *, and it will also be GC under certain conditions. When the memory required by the method area exceeds its allowed size, an OutOfMemory error message will be thrown.

Third, solutions to common memory overflow errors

1, OutOfMemoryError exception

In addition to program counters, there may be out-of-memory (OOM) exceptions in several other runtime areas of virtual machine memory.

Java heap overflow

General exception information: java.lang Out of memory error: java heap space.

Java heap is used to store object instances. As long as we keep creating objects, ensure that there is an accessible path between GC root and objects, and prevent garbage collection mechanism from clearing these objects, when the number of objects reaches the maximum heap capacity limit, a memory overflow exception will occur.

This kind of exception usually appears through memory mirror analysis tools (such as Eclipse Memory).

Analyzer) for snapshot analysis of dump heap, the key is to confirm whether the objects in memory are necessary, and first distinguish whether they are due to memory leakage (memory

Leak) or memory overflow.

If it is a memory leak, you can further view the reference chain from the leaked object to the GC root through tools. So we can find out how the leaked object is associated with the GC root, and the garbage collector can't automatically recycle it.

If there is no leakage, you should check whether the parameters of the virtual machine (-Xmx and -Xms) are set correctly.

2. Virtual machine stack and local method stack overflow

If the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine, a StackOverflowError exception will be thrown.

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

It should be noted that the larger the stack, the fewer threads can be allocated.

3. Runtime constant pool overflow

Exception message: java.lang Out of memory error: perm genspace

If you want to add content to the runtime constant pool, the easiest way is to use the native method String.intern (). The function of this method is: If the pool already contains a value equal to.

The String of this string returns a String object representing this string in the pool; Otherwise, the String contained in this string object is added to the constant pool and this string is returned.

A reference to an object. Because the constant pool is allocated in the method area, we can use -XX:PermSize and -XX:MaxPermSize to limit the size of the method area, thus indirectly limiting the constants in it.

Capacity of the pool.

4. Method area overflow

The method area is used to store the related information of the class, such as class name, access modifier, constant pool, field description, method description, etc.

Exception message: java.lang Out of memory error: perm genspace

Method area overflow is also a common memory overflow exception. If a class is to be recycled by the garbage collector, the judgment conditions are very harsh. Pay special attention to this point in applications that often generate a large number of classes dynamically.