Diagnosis of Memory leakage in Java and diagnosis of Java Memory leakage

Source: Internet
Author: User

Diagnosis of Memory leakage in Java and diagnosis of Java Memory leakage

Every time I suspect a memory leak, I Have To rummaging through these commands. So, here we will summarize it for future use:

First, I use the following command to monitor the process:

1 while ( sleep 1 ) ; do ps -p $PID -o %cpu,%mem,rss  ; done

(New Relic is available if any)

If you see that the memory is increasing rapidly, it may be because of virtual machine settings. If you do not explicitly specify the JVM memory settings, it will set the default value to them. To obtain the default value, run the following command:

1 java -XX:+PrintFlagsFinal -version | grep -i HeapSize

If none of these meets your expectation, You need to specify the JVM memory settings. You can use the following command to set the minimum and maximum heap sizes:

1 java -Xms128m -Xmx256m

Although you have reasonable memory settings and can monitor the process, you may still see that the memory increases over time. To further explore the cause, you can use the following command to view the histogram of the object instance:

1 jmap -histo $PID

If you still do not have enough information, run the following command to perform a heap dump:

1 jmap -dump:format=b,file=/tmp/dump1.hprof $PID

In general, I will use two heap dump and then compare them using the following jhat command:

1 jhat -baseline /tmp/dump1.hprof /tmp/dump2.hprof

This command starts an HTTP server and you can use it to explore the difference between the two heap dumps. By default, the HTTP server starts port 7000. You can access this port in a browser.

If you have a firewall that can be accessed through SSH, you can use the following command to connect to the Port:

1 ssh -L 7000:localhost:7000 $HOST

Scroll down to the bottom of the first page and you will see two useful links:

  • Show heap histogram
  • Show instance counts for all classes (excluding platform)

This will show you all the "new" instances between different heap dump, and should help you detect where the leak comes from. As follows:

Then you have a magic command line to quickly view the directory, so that you can use it when you need to diagnose memory leaks (but I always forget ).

Two days ago, I saw someone in a group who recommended an app to ask questions. They could answer questions, just like uber Didi took a taxi, generally, this kind of software throws money to give a red envelope. Brother has experience in using uber tickets before! I have tried it several times and I can use it to cash out the registered red envelopes and the money I received before. I have raised more than 50 yuan, and I can use it in visual testing. Ps, but try to ask technical questions, otherwise it will be easily blocked.
If you have any technical skills, you can try it yourself. If you do not, you can ask me 315414695: QQ Group 290551701 has gathered many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!

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.