Is there a way to start or stop collecting data from a profiled application?

I would like to profile a certain part of the execution of a small application. Is there a way to initiate data collection in VisualVM from an application that needs to be profiled? In other words, is it possible to add code to a profiled application so that profiling in VisualVM starts automatically after this code?

meaninglessCode(); // How to start data collection? com.sun.tools.visualvm.....startSampling(); // codeToBeProfiled(); // How to stop data collection? com.sun.tools.visualvm.....stopSampling(); // furtherMeaninglessCode(); 

My current workaround is to call Thread.sleep , which gives me time to press a button in the VisualVM GUI, but this is far from satisfactory. Several Google searches did not show any success. I appreciate any hint.

+4
source share
1 answer

You can use two VisualVM options to set up a profiling session, as described in this blog post :

  • Profiling Roots: Enter a class containing codeToBeProfiled()
  • Toolkit filters: specify your own or leave the default value

Thus, VisualVM will begin profiling when any class method containing codeToBeProfiled() .

+1
source

All Articles