How to profile my code

A few years ago I was doing some JVM profiling and at the time a lot of the sources I found recommended VisualVM. It used to be standard with the JDK download. Looks like it went away after Java 8, but you can still get it at https://visualvm.github.io/. The biggest downside was that it was a GUI that you needed to connect to a process to profile. This is fine for long-running programs like servers, but it doesn’t work for short scripts. On the plus side, the VisualVM seemed to have a much lower performance impact than hprof. My memory is that running my program under hprof made it go something on the order of 10x slower than a normal run but connecting the VisualVM caused less than a 2x performance hit.