Use mat to analyze memory usage when Java programs run

Source: Internet
Author: User
Tags jconsole

Java appears outofmemoryerror or found that Java applications occupy a very unusual memory, then we generally use the following steps to analyze:
A. Dump the heap used by Java applications
B. Use the Java Heap Analysis tool to identify suspect objects that have exceeded the expected memory footprint
C. According to the situation, the reference relationship between the suspect and other objects is analyzed.
D. Analyze the source code of the program to find out why there are too many suspects.
Take the following code as an example:
public class TObject {int[] arr = new int[20000];  }  public class Test1 {private list<tobject> List = new arraylist<tobject> ();p ublic void Test () {int i = 0;while (true) {try {thread.sleep (100);//If the loop limit is not added, it probably consumes 32M heapif (i>350) {continue;} when looping to more than 380.} List.add (New TObject ()); i++; System.out.println ("There is" + i + "objects created");} catch (Interruptedexception e) {e.printstacktrace ();}}} public static void Main (string[] aregs) {new Test1 (). Test ();}}
The heap dump used by Java applications can have the following methods:
1, set the JVM to automatically generate dump files when out of memory:
For example JAVA-XMS32M-XMX32M-XX:+HEAPDUMPONOUTOFMEMORYERROR-CP Test.jar com.test.Test1

Ctrl+break can generate dump files
It is said that this parameter is no longer supported on JDK6 versions.
JAVA-XMS32M-XMX32M-XX:+HEAPDUMPONCTRLBREAK-CP Test.jar Com.test.Test1
You can try.
JAVA-XMS32M-XMX32M-XRUNHPROF:FORMAT=B,FILE=HEAPDUMP.HPROF-CP Test.jar Com.test.Test1
However, these two methods are not recommended for use
2, use the tool that comes with the JDK:
2.1 Jconsole
Launch Jconsole.exe and Invoke Operation Dumpheap () on Com.sun.management.HotSpotDiagnostic MBean
Definition of method: Dumpheap (String OutputFile, Boolean live)

2.2 JPS and Jmap
Run JPS first to get the Java process ID
C:\temp\test>jps
8208 Test1
4236
7440 Jps
Jmap-dump:file=test2.hprof 8208
You can refer to the following links for more information:
Http://wiki.eclipse.org/index.php/MemoryAnalyzer#Getting_a_Heap_Dump

The Java heap analysis tool can use the mat
Installing the Mat Eclipse plugin
mat-http://archive.eclipse.org/mat/1.0/update-site/
Use MAT to analyze suspicious memory leaks, such as
Shallow heap means the memory bytes occupied by the current object in the heap
The retained heap means that all objects referenced by the current object are in memory bytes occupied by the heap

The following results can be obtained from the analysis:
Com.test.test1-->java.util.arraylist->java.lang.object on the main thread (ArrayList bottom
The object is stored in an array)--there are a number of com.test.TObject instances.

In addition, Mat can also directly analyze the JVM of this machine, such as


In addition, Jhap can also be used for analysis: http://blog.csdn.net/kkdelta/article/details/7851467

Use mat to analyze memory usage when Java programs run

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.