9

Briefly reading tutorials and playing with JVisualVM, I can manually attach it to a long-running server or Swing application, but I'd also like to use it for short programs that run in under a second.

I suppose I could have my program block on the console, attach JVisualVM, and then hit a key to have the program do its real work. Is there a less kludgy workaround?

(This is mainly for my own curiosity, and it would be nice to use a polished GUI like JVisualVM instead of -Xprof or HPROF).

ide
  • 19,942
  • 5
  • 64
  • 106
  • 1
    possible duplicate of [how to profile application startup with visualvm](http://stackoverflow.com/questions/4434860/how-to-profile-application-startup-with-visualvm) – Michael Borgwardt Mar 05 '11 at 00:46

2 Answers2

3

If you are using eclipse, you can use the "Stop in main" option in the debug mode and add the following lines as arguments to your process

-Dcom.sun.management.jmxremote.port=<fixed_port_number>
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.authenticate=false

Once it starts in debug mode and blocks then attach the jvisualvm to it connecting to the process using the port number specified.

Kilokahn
  • 2,281
  • 1
  • 25
  • 50
2

Add suspend=y to your debug settings. This will cause your program to halt until a debugger/profiler is attached.

Ruben
  • 9,056
  • 6
  • 34
  • 44
  • Some questions(http://stackoverflow.com/questions/7096121/profile-entire-java-program-execution-in-visualvm) indicate that the suspend option doesn't work – Kilokahn Jul 17 '12 at 03:41