59

As far as i can tell, you can only profile a running application using VisualVM.

Does anyone know of a way to profile the launch and startup of a java application using VisualVM?

I'm convinced there must be a way, otherwise it would be a major oversight.

Hoping I've just misread the documentation.

Thanks, p.

skaffman
  • 398,947
  • 96
  • 818
  • 769
pstanton
  • 35,033
  • 24
  • 126
  • 168

4 Answers4

19

Use the eclipse launcher, and set a breakpoint at an appropriate place in the main method.

Then, start in debug mode, enable profiling in visualVM, and then resume using eclipse.

That won't profile class loading and stuff, but it's good enough for me.

KarlP
  • 5,149
  • 2
  • 28
  • 41
13

Are you setting up the profiling using the `-Xrunjdwp" command-line option? If so, that option has a "suspend" parameter for just this purpose:

True if the target VM is to be suspended immediately before the main class is loaded; false otherwise.

Example from my own config:

-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

I use this with the YourKit profiler, but I think the principle is the same.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • i am not using the Xrunjdwp option, should i be? the default value for suspend is 'true', isn't that correct? – pstanton Dec 14 '10 at 01:18
  • @pstanton: I'm not really sure about the whats-and-wheres of `Xrunjdwp`. I suggest reading up on it, see if you can use it. – skaffman Dec 14 '10 at 01:30
  • @pstanton: What is the exact -Xrunjdwp command you ended up using? When I try to use it with VisualVM, VisualVM is no longer able to properly connect to the JVM process. – Jim Hurne Mar 04 '11 at 13:43
  • sorry jim, i gave up. i actually wrote some code so that my program paused until a button was clicked .. which i'd click after i'd attached the profiler ;) – pstanton Mar 08 '11 at 10:49
  • 2
    However, answers elsewhere claim that `-Xrunjdwp` does not help. See http://stackoverflow.com/questions/7096121/profile-entire-java-program-execution-in-visualvm – Raedwald Nov 23 '11 at 16:39
  • the `jdwp` in `-Xrunjdwp` stands for **Java Debug Wire Protocol**. Notice the word **Debug** – Ahmad Tn Jul 08 '19 at 12:21
10

I found Startup Profiler helpful:

Startup Profiler plugin for VisualVM 1.3.6 enables instrumented profiling of local Java 5+ applications from their startup. It is also helpful when profiling short-running processes.

flaviut
  • 2,007
  • 3
  • 23
  • 32
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
  • 9
    This ended up working for me, but make sure you download the separate VisualVM application and don't try to reuse the one that comes with the JDK; for some reason, the Startup Profiler plugin isn't available for the stock JDK version. – Max May 15 '15 at 02:43
0

Both common Java development environments (NetBeans/Eclipse) support application execution directly with attached VisualVM. In case of Eclipse you have to install a plugin. In case of NetBeans the support is AFAIK already integrated.
Even if do not have the sources you can still run the JAR file(s) via both environments and therefore start your app connected with VisualVM.

Robert
  • 39,162
  • 17
  • 99
  • 152