Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profile Entire Java Program Execution in VisualVM

In Java profiling, it seems like all (free) roads nowadays lead to the VisualVM profiler included with JDK6. It looks like a fine program, and everyone touts how you can "attach it to a running process" as a major feature. The problem is, that seems to be the only way to use it on a local process. I want to be able to start my program in the profiler, and track its entire execution.

I have tried using the -Xrunjdwp option described in how to profile application startup with visualvm, but between the two transport methods (shared memory and server), neither is useful for me. VisualVM doesn't seem to have any integration with the former, and VisualVM refuses to connect to localhost or 127.0.0.1, so the latter is no good either. I also tried inserting a simple read of System.in into my program to insert a pause in execution, but in that case VisualVM blocks until the read completes, and doesn't allow you to start profiling until after execution is under way. I have also tried looking into the Eclipse plugin but the website is full of dead links and the launcher just crashes with a NullPointerException when I try to use it (this may no longer be accurate).

Coming from C, this doesn't seem like a particularly difficult task to me. Am I just missing something or is this really an impossible request? I'm open to any kinds of suggestions, including using a different (also free) profiler, and I'm not averse to the command line.

like image 338
kbolino Avatar asked Aug 17 '11 16:08

kbolino


People also ask

What is profiler in VisualVM?

Java VisualVM enables you to take profiler snapshots to capture the results of a profiling session. A profiler snapshot captures the results at the moment the snapshot is taken. To take a snapshot, click the Take Snapshot of Collected Results button in the toolbar while a profiling session is in progress.

Is JVM a Java profiler?

A Java Profiler is a tool that monitors Java bytecode constructs and operations at the JVM level. These code constructs and operations include object creation, iterative executions (including recursive calls), method executions, thread executions, and garbage collections.

What is difference between JConsole and VisualVM?

VisualVM takes application monitoring one level deeper than JConsole by allowing the user to analyze thread execution as well as the ability to profile CPU and memory usage of JVM requests, both of which are triggered manually by the user.


1 Answers

Consider using HPROF and opening the data file with a tool like HPjmeter - or just reading the resulting text file in your favorite editor.

Command used: javac -J-agentlib:hprof=heap=sites Hello.java  SITES BEGIN (ordered by live bytes) Fri Oct 22 11:52:24 2004           percent          live          alloc'ed  stack class  rank   self  accum     bytes objs     bytes  objs trace name     1 44.73% 44.73%   1161280 14516  1161280 14516 302032 java.util.zip.ZipEntry     2  8.95% 53.67%    232256 14516   232256 14516 302033 com.sun.tools.javac.util.List     3  5.06% 58.74%    131504    2    131504     2 301029 com.sun.tools.javac.util.Name[]     4  5.05% 63.79%    131088    1    131088     1 301030 byte[]     5  5.05% 68.84%    131072    1    131072     1 301710 byte[] 

HPROF is capable of presenting CPU usage, heap allocation statistics, and monitor contention profiles. In addition, it can also report complete heap dumps and states of all the monitors and threads in the Java virtual machine.

like image 84
Amir Afghani Avatar answered Oct 10 '22 19:10

Amir Afghani