JVM: Memory monitoring means and memory overflow resolution for each area

Source: Internet
Author: User

Reprint please be sure to indicate the source: Jiq Technical Blog-Jiyichin


Introduction

This article focuses on some common virtual machine memory monitoring means, as well as the JVM runtime data area each part of the memory overflow occurrence and corresponding solution, overall, is a general summary, involving relatively not very deep, the purpose is to let oneself and other beginners have a framework, conceptual understanding, when encountered problems have traces, Don't be overwhelmed.


one, virtual machine memory monitoring means

common problems with virtual machines include: memory leaks, memory overflows, frequent GC resulting in performance degradation, the causes of these problems can be analyzed by the following virtual machine memory monitoring means, the implementation may need to be flexible choice, while using two or more means to analyze together.

GC logs, for example, can analyze which GCs are more frequently causing performance degradation and memory leaks. Similar to GC logs, the Jstat tool can also be used to view GC conditions and analyze whether a memory leak has occurred. After you have determined that a memory leak occurs, you can view a snapshot of the virtual machine memory through a combination of analysis tools such as the Jmap tool and mat to analyze the cause of a memory leak. Memory overflow snapshots can be used to analyze the causes of memory overflow occurrences.


GC Log records

By observing the GC log, the frequency of GC, and the memory of which areas are reclaimed each time the GC is recorded, the JVM-related settings are adjusted based on this information, which can be used to reduce the rate of minor GC and the number of full GC. You can also determine if a memory leak occurs.

The following are the common GC log output parameters:

U-VERBOSE.GC: Displays the action content of the GC. Open it to show when the busiest and most idle collection behavior occurred, the amount of memory before and after collection, the time it took to collect, and so on.

U-xx:+printgcdetails: Learn more about the changes in the GC.

U-xx:+printgctimestamps: Understand when garbage collection occurs, measured in seconds since the JVM started.

U-XX:+PRINTHEAPATGC: Get more detailed information about the heap.

U-xloggc:[file]: Output GC information to a separate file


Jstat: Virtual Machine Statistics monitoring tool

Real-time monitoring of the virtual runtime of the class load, the various parts of the memory consumption, GC situation, JIT compilation and so on.

Example: Every 250ms query process 2211 garbage collection situation, query 50 times

Step ①:JPS List all running JVM instances of this machine, get the PID of the JVM

Step ②:jstat Real-time monitoring GC situation, JSTAT–GC 2211 250 50

Other parameters include:

-class Monitor class load, unload quantity, total space, and time spent on class loading

The-gccapacity monitoring content is the same as the-GC, and the output focuses on the maximum and minimum space used in each area of the heap

The-gcutil monitor is identical to the-GC, and the output focuses on the percentage of total space occupied by each area of the heap

-gcnew monitoring of the new generation GC

-gcold monitoring of old generation GC


Jmap: Virtual machine memory imaging tool

The Jmap tool allows a running JVM to generate a dump file that can generate a snapshot through Jmap when there is a problem with the JVM's memory, analyzing the entire heap in two steps:

Step 1:JPS List all running JVM instances of this machine, get the PID of the JVM

Step 2: Use the JMAP command to export the specified JVM snapshot as a dump file

Jmap-dump:format=b,file=path/heap.bin PID

After obtaining a dump file for a JVM snapshot, you can analyze it through the mat tool.

The MAT tool is a plugin for eclipse, which is very handy, especially when analyzing large memory dump files, it is very intuitive to see the amount of memory that each object occupies in the heap space, the number of class instances, the object reference relationship, the Memoryanalyzer With OQL object queries, and the ability to easily find information about the object GC roots, it is most appealing to quickly generate a memory leak report for developers to easily locate and analyze problems.

In addition, Jmap can query the details of the Finalize execution queue, Java heap, and persistent generations, such as space usage, which collector is currently in use, and so on.


Memory Overflow Snapshot generation

By setting the JVM parameters, you can automatically generate a dump file when a virtual machine OutOfMemoryError (OOM) memory overflows, and by analyzing the dump file to see memory usage, you can find out why a memory overflow occurred:

-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=/path/to/heap/dump

After obtaining a dump file for a JVM snapshot, you can analyze it through the mat tool.


second, run-time data area memory overflow

The JVM runtime data area is divided into the following sections:

Where the method area and heap are shared by all worker threads, and the stack, program counter, and local method stack are thread-private.


Note: Images are transferred from the network


1. Program counter

function: The address of the next byte-code instruction to be executed to the current thread

Memory Overflow: does not occur


2. Virtual Machine stack

function: consists of stack frames, each stack frame represents a method call, which contains the storage variable table, the operand stack and the method exit three parts, the stack frame will be ejected after the execution of the method is completed.

Memory Overflow: Stackoverflowerror and OutOfMemoryError.

Cause of overflow:

stackoverflowerror: If the requested stack depth is greater than the depth allowed by the virtual machine, this exception will be thrown, and if you use the virtual machine default parameters, the depth of 1000 to 2000 is generally no problem.

OutOfMemoryError: Because the heap memory and the method area capacity are removed, the remaining memory is partitioned by the virtual machine stack and the local method stack, and if the remaining memory is not sufficient to meet the running of more worker threads or to expand the virtual machine stack, The OutOfMemoryError exception is thrown.

Workaround:

For Stackoverflowerror:

1. First, the stack overflow will output the exception information, according to the information to see whether the corresponding method calls the infinite call, or stack frame too large and other code logic problems, by modifying the code logic to solve;

2. If you really need a larger stack capacity, you can check and adjust the stack capacity:-xss16m.

For OutOfMemoryError:

1. First check whether to create too many threads and reduce the number of threads

2. Can be resolved by "reduce the maximum heap capacity" or "reduce stack capacity".


3. Local Method Stack

role : The only difference from the virtual machine stack is that the virtual machine stack executes the Java method, while the local method stack executes the local C/S method

Memory Overflow : Stackoverflowerror and OutOfMemoryError

overflow reason : Same as virtual machine stack

workaround : Same as virtual machine stack


4. Heap

role : All threads are shared, holding object instances

Memory Overflow : Outofmemoryerror:java heap Space

overflow reason : There is not enough memory in the heap to complete the instance assignment and cannot continue to expand

Workaround :

1. First through the "Memory overflow snapshot + mat analysis tools" to analyze the existence of memory leaks, check the suspicious points such as collections, third-party libraries such as the use of database connections, new keyword-related.

2. If there is no memory leak, then it is memory overflow, all objects are still need to survive, this time can only adjust the heap of memory:-xms and-XMX.


5. Method area

role : All threads are shared, storing loaded class information, constants, static variables, and immediately compiled code

Memory Overflow : Outofmemoryerror:permgen space

overflow Reason : The method area does not have enough memory to complete the memory allocation store run the newly loaded class information

Workaround :

1. Check whether to load too many class files (jar files), or repeatedly load the same class file (jar file) multiple times

2. Change the size of the method area by-xx:permsize=64m-xx:maxpermsize=128m


6. Run a constant-rate pool

function : Part of the method area, storing constants

Memory Overflow : Outofmemoryerror:permgen space

overflow Reason : The method area does not have enough memory to complete the memory allocation, the store runs the newly created constant, such as The Intern () method of the string class, whose purpose is to return its reference if the constant pool already contains an identical string. Otherwise, the string contained in this string object is added to the constant pool.

Workaround :

1. Check if too many constants are created

2. Change the size of the method area by-xx:permsize=64m-xx:maxpermsize=128m


7. Direct Memory

function: does not belong to the JVM runtime data area, nor is it a memory area defined in the virtual machine specification. The NIO introduced by JDK1.4 contains channel channels and buffer buffers, and the application obtains data from the channel through the kernel buffer of the OS and then to buffer, because it is time consuming, so buffer provides a way to manipulate the operating system buffers directly, i.e. Bytebuffer.allo Catedirector (size), this method returns the Directbytebuffer application, which is the buffer associated with the underlying storage space, that is, direct memory.

Memory Overflow : OutOfMemoryError

overflow reason : JVM required memory + Direct Memory > Machine physical memory (or OS level limit), unable to dynamically expand

Judging methods: For example, high memory consumption, machine performance dips, but through GC information or JSTAT found that the GC is very few, through the Jmap to obtain snapshot analysis and no anomalies, and the program is directly or indirectly used in the NIO, and may be direct memory leaks.

workaround : Analyze NIO-related program logic solutions.


JVM: Memory monitoring means and memory overflow resolution for each area

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.