Install the Memoryanalyzer plugin and use it in eclipse

Source: Internet
Author: User

Introduction to installing Memoryanalyzer plug-in in eclipse

Eclipse is a very useful IDE for Java, and its own extensible plug-in is very helpful to the productivity of Java programmers.

Memoryanalyzertool (also known as MAT) is a Java Virtual machine memory image analysis tool that allows programs to throw exceptions when the Java program is running, plus parameters that have already been set (-xx:+ heapdumponoutofmemory) debug the memory leak or the location of the exception and cause tracking, Memeoryanalyzer can analyze the dump out of the snapshot, the focus is to confirm whether the object in memory is necessary, The first is to determine whether there is a memory leak (Leak) or memory overflow (OverFlow).

Ii. preparatory work

1.Eclipse IDE: My is installed Eclipse Neon3, most versions of Eclipse support plug-in installation.

Name: Eclipse Neon

Version: 3

: Https://www.eclipse.org/downloads/packages/release/neon/3 (mine is the Java EE version of Windows64 bit)

2.Memory Analyzer Tool (MAT): Since Eclipse supports the quick installation from the URL import package, as well as the import installation after downloading the package, the download package will be available on the Eclipse website:

Name: Memory Analyzer

Version: Latest Version 1.7.0.20170613

Official website: https://eclipse.org/mat/downloads.php

There are three kinds:

1.Eclipse imported URL, features: Simple and fast installation, do not need to manually download, direct input URL by eclipse to help you automatically download;

2.Eclispe import Zip, features: self-download, when the Eclipse breeding tool download slow time can use their own download and then import zip;

3. Independent Memoryanalyzer Tools, features: Self-download, not dependent on the launch of Eclipse, can be directly imported. hprof file for analysis.

Iii. Start installation of Mat

Run eclipse, and then click "Help=" on the menu bar "Install New software ..."

Enter the software source import interface, you can choose to use the URL to import directly, URL import address is:

http://download.eclipse.org/mat/1.7/update-site/

However, due to the instability of the mirror connection, I used to download the ZIP archive and then install the package: Http://ftp.jaist.ac.jp/pub/eclipse/mat/1.7/MemoryAnalyzer-1.7.0.201706130745.zip

After the download is complete, import it, "Add" Archive "Select your downloaded zip file" OK ":

After determining the two items under "Memory Analyzer for Eclipse IDE", this is a plugin for internal use of eclipse, the following "stand-alone Memory Analyzer" is an independent analysis tool, usually not checked.

After clicking "Next" to wait for a period of time after the eclipse computing requirements and dependent files have been downloaded, all the way "Next" and accept the agreement "finish" to complete the installation, after restarting the ECLISPE you can use the mat.

Iv. simple use of mat 1. Set JVM parameters

The parameters of the JVM can be used to debug common bugs in the JVM under various restrictions, and briefly introduce several parameters:

-xx:+heapdumponoutofmemoryerror    // VMS generate a snapshot of the storage on heap exceptions, the suffix is generally. hprof-xms    //  The minimum value of the Java heap, such as-xms20m, sets the minimum value of the Java heap tothe maximum value of 20MB-xmx    //Java heap, such as-xms40m, which sets the maximum value of the Java heap to 40MB

Once you know the parameters, you can set the parameters so that we simply use the mat tool for testing and set the parameters as follows:

-xx:+heapdumponoutofmemoryerror-xms20m-xmx20m

The minimum and maximum heap is set to avoid automatic scaling of the heap, and 20MB of space is designed to allow the program to fill up as quickly as possible to make memory overflow.

Setting the JVM's startup parameters can be completed in "Run" Run Configuration Arguments "Vm Arguments"

2. Write memory Overflow (Leak) code

Idea: By creating a new collection list, and then looping in a while (true), the list collection keeps adding new objects, making it continuously fill up the heap space

Code:

 PackageCom.study;Importjava.util.ArrayList;Importjava.util.List; Public classTeststudy {Static classOomclass {} Public Static voidMain (string[] args) {List<OOMClass> list =NewArraylist<oomclass>();  while(true) {List.add (NewOomclass ()); }    }}

Execute the code and find the output as follows:

Note the "java_pid24792.hprof" file, which is a JVM-generated pair of storage snapshots that can then be opened by the Mat tool for analysis tracking, which is automatically saved to your working directory under Eclipse workspace , Take my example:

3. Open the mat's perspective

Since the mat tool needs to open a different work view (just like the switch view of Eclipse EE and Java), it needs to be configured to open "window" perspective "open perspective" other ... ":

Select the memory analysis and determine:

After that, you'll go to the Mat workspace view and you can switch back to Java or EE in the upper right corner of Eclipse, so you won't be able to cut back to Java until you're done with mat analysis.

4. Open the heap dump snapshot you want to analyze. hprof file

Since the mat is dependent on the generated heap dump snapshot instead of being automatically captured at run time, after confirming the work space into the mat, click "File" Open heap dump to select the "java_pid24792.hprof" file that was just generated. By default, you can view the analysis chart of memory problems and the GC root

You can do some analysis and see a few options under action:

A.histogram: Lists the instances of each class;

B.dominator: List the largest objects and what they exist;

C.top Comsumer: Print the most expensive objects grouped by class and package;

D.duplicate Classes: Detects classes loaded by multiple ClassLoader.

5. Use the mat to find the object that the problem exists

Because it is a simple problem, so we directly select Dominator, can directly print out the percentage of the object as a percentage, the general memory overflow in most cases there is an object too many exist and not be caused by GC recycling, so you can open the maximum percentage of class-level search, It's not hard to find the problem:

You can see 25 of the listed amounts, and 810,300 items are not listed, these 810,000 objects occupy 97.36% of the heap space, so it is not difficult to find that the object generated by the Oomclass class is too large to be reclaimed causing memory overflow.

6. Further trace the source of the problem and locate it

In the tab page of "Default_report org.eclipse.mat.api:suspects"

Track error points in "Thread Stack" after "program Suspect 1 Details" click

See the error point in line 13th, find the corresponding Java code teststudy 13 lines:

1  PackageCom.study;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6  Public classTeststudy {7 8     Static classOomclass {}9     Ten      Public Static voidMain (string[] args) { Onelist<oomclass> list =NewArraylist<oomclass>(); A          while(true) { -List.add (NewOomclass ()); -         } the     } -  -}

The error location of the trace is found, and the above is the stack analysis done using the Memory Analyzer tool.

Install the Memoryanalyzer plugin and use it in eclipse

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.