Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use hprof

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use hprof", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use hprof" this article.

1. Sample demonstration

Example program:

Note: this is a piece of test code that delays through the sleep method, which is very slow in the process of running the program. I want to know which part of the program affects the overall performance?

I added the following running parameters to the Java program:

-agentlib:hprof=cpu=times,interval=10

/ * the execution time hprof=cpu of the times:java function is 10 samples for the cpu statistical time interval=10 * /

Running the program again shows the following figure:

At this time, it was also found that there was an extra text file java.hprof.txt in the project directory.

The contents are as follows:

Note: from the above, we can see which kind of method takes a long time to execute and consumes cpu time, which is clear at a glance, which is convenient for us to quickly locate the problem.

2. The specific explanation of the order

Hprof is not an independent monitoring tool, it is just a Java agent tool that can be used to monitor the CPU information and heap content of Java applications while they are running. Use the java-agentlib:hprof=help command to view hprof usage documents.

From the figure above, you can see that this tool is very powerful and there are a lot of things that can be counted. The above example counts cpu time, and we can also count the dump information of memory usage.

For example:-agentlib:hprof=heap,format=b,file=/test.hprof

This hprof gadget is very convenient for us to use when using JUnit self-test code, it can not only solve the business BUG, but also solve the performance problems that can be found in certain programs, which is very practical.

Second, performance troubleshooting tool-pidstat

1. Sample demonstration

Example program:

Note: this is a test java program, run it.

On the command line, enter:

Pidstat-p 843 13-u-t

/ *-u: monitoring parameter 1 / 3 for cpu utilization: sampling once per second for a total of three times-t: refine the monitoring level to thread * /

The run command appears as shown in the following figure:

Note: in fact, TID is thread ID,%usr represents the user thread utilization, you can see from the figure 855 this thread occupies a very high cpu.

Then enter the following command:

Jstack-l843 > / tmp/testlog.txt

Look at the testlog.txt and display the following parts:

Note: we focus on the NID field of the log file, which corresponds to the hexadecimal representation of TID that we mentioned above. Convert the above decimal 855 to hexadecimal to 357. Search in the log and see the following:

From this, program points with performance bottlenecks can be inferred.

2. Detailed explanation of pidstat specific commands

Pidstat is a very powerful performance monitoring tool, it is one of the components of Sysstat, which can be downloaded from http://sebastien.godard.pagesperso-orange.fr/download.html and installed through commands such as. / configure. The power of this command is that it can monitor not only the performance of the process, but also the performance of the thread.

The common display fields of pidstat monitoring cpu are as follows:

The fields commonly used in pidstat monitoring io are as follows:

Third, a case analysis of memory overflow

1. Memory overflow

There are eight servers in the system, and only one server at random reports a java.lang.OutOfMemoryError: GC overhead limit exceeded error, followed by a memory overflow error java.lang.OutOfMemoryError: Java heap space.

2. Theoretical support.

Let's first explain what a GC overhead limit exceeded error is.

GC overhead limt exceed check is a policy defined by Hotspot VM 1.6. it predicts whether or not to OOM by counting the GC time and throws an exception in advance to prevent the occurrence of OOM. Sun's official definition of this is: "the parallel / concurrent collector throws an OutOfMemroyError when the GC collection time is too long. The definition of too long is that more than 98% of the time is spent doing GC and less than 2% of the heap memory is reclaimed. It is used to prevent applications from working properly due to too small memory.

You can see that when objects in the heap cannot be recovered, such an error is alerted in advance, and there is no memory overflow, which is added by default in JDK.

3. DUMP file analysis

Import the dump file into the VisualVM tool, as shown in the following figure:

From the above figure, we can see that in the class structure diagram, the three items that take up the most memory are char [], LinkedHashMap and String. However, the number of instances of these three items is not full, it seems that there will be no memory overflow, how can we analyze it specifically? The reason is GC overhead limt exceed, this error will not be reported until the real memory overflow, so through the dump file, we can only judge and analyze which items may cause overflow. When we enter the char [] item, we will find that a lot of hessian url characters are cached. Through the exclusion program, we can see that because the underlying middleware program caches each call url in order to improve "performance". It does not have to be generated every time, but there is no corresponding cache release operation, which causes a large number of character objects to hold for a long time and report an error. Here, we will not take a screenshot to look at the code, which involves some company information.

4. Solution to the problem

* you can add the startup parameters of JVM to remove the early alarm limit:-XX:-UseGCOverheadLimit, as it allows the application to alarm in advance every time, it is better to let the storm come more violently and directly overflow memory, because the server is a cluster, and one of them will not affect the normal online transactions. At the same time, it is convenient for us to sort errors through logs.

Through the troubleshooting program, check to see if the system has code that uses large memory not to release or loop.

The above is all the contents of this article "how to use hprof". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report