The problem of Java memory leak investigation and localization

Source: Internet
Author: User

1. Why memory leaks occur

How does Java detect internal leaks? We need some tools to detect and find memory leaks, otherwise it's easy to have a down-machine problem.

The most convenient place to write Java programs is that we do not need to manage the allocation and release of memory, everything is handled by the JVM, when the Java object is no longer applied, when the heap memory is not enough, the JVM will garbage collection, clean up the heap memory of these objects, if the object has been applied, The JVM cannot reclaim it, and when a new object is created, it cannot get enough memory from the heap to allocate to the object, which can cause memory overflow. Where memory leaks occur, it is common to keep objects in containers that do not have a corresponding size limit or cleanup mechanism. Easy to cause memory overflow.
How to quickly locate a problem when the server application is taking up too much memory? Now, the advent of Eclipse mat makes the problem very simple. Eclipsemat is a well-known SAP company contribution tool that can be downloaded to it on the Eclipse website, completely free of charge.
To locate the problem, first you need to get a memory snapshot of the server JVM. The JDK's own jmap can take a snapshot of the memory at a moment, and after exporting it to the DMP file, it can be analyzed with eclipse mat to find out if that object uses too much memory.

2, Memory leakage phenomenon:

Often, the initial signs of a program's memory leak occur after an error and get a outofmemoryerror in your program. This typically happens in a production environment where you want to have as little memory leaks as possible and minimize the likelihood of debugging. Perhaps your test environment and the product's system environment are different, resulting in leaks will only be exposed in the product. In this case, you need a low-load tool to monitor and look for memory leaks. At the same time, you need to connect the tool to your system without restarting or mechanization of your code. Perhaps more importantly, when you do the analysis, you need to be able to separate the tools so that the system will not be disturbed.
A outofmemoryerror is often a sign of a memory leak, and it is possible that the application does use too much memory; this time, you can neither increase the number of JVM heaps, nor change your program to make him reduce memory usage. However, in most cases, a outofmemoryerror is a memory leak flag. One solution is to continue to listen to GC activity, to see whether the memory usage increases over time, and if so, there must be a memory leak in the program.

3. Discovery of memory leaks

1. JSTAT-GC PID

You can display GC information, the number of times the GC is viewed, and the time.

The last five items were the number of young GC, the time of young GC, the number of full GC, the time of full GC, and the total time of GC.

2.jstat-gccapacity PID

can display the use and occupancy of three generation (Young,old,perm) objects in VM memory,

For example: PGCMN shows the minimum perm memory usage, PGCMX shows the maximum memory usage of perm,

The PGC is the current newly generated perm memory footprint, and the PC is but the pre-perm memory footprint.

The other can be based on this analogy, OC is the old inside the pure consumption.

3.jstat-gcutil PID

Statistical GC information Statistics.

4.jstat-gcnew PID

Information about the young generation object.

5.jstat-gcnewcapacity PID

The information of the young generation object and its consumption.

The problem of Java memory leak investigation and localization

Related Article

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.