I profile running Java applications often with VisualVM but it needs X to run on the machine.
I know I can connect through management port but that will be an offlin
The most precise profiling can be achieved with https://github.com/jvm-profiling-tools/async-profiler.
This project is a low overhead sampling profiler for Java that does not suffer from Safepoint bias problem. It features HotSpot-specific APIs to collect stack traces and to track memory allocations. The profiler works with OpenJDK, Oracle JDK and other Java runtimes based on HotSpot JVM.
Here is my script to install and run it from command-line:
async-profiler.sh
if [ ! -d profiler ]; then
mkdir profiler && cd profiler && curl -L https://github.com/jvm-profiling-tools/async-profiler/releases/download/v1.6-ea/async-profiler-1.6-ea-linux-x64.tar.gz | tar xvz
echo 1 > /proc/sys/kernel/perf_event_paranoid
echo 0 > /proc/sys/kernel/kptr_restrict
#apt install openjdk-8-dbg
else
cd profiler
fi
#jps
./profiler.sh -d 60 -f dump_`date +%Y-%m-%d_%H-%M-%S`.jfr `jps -q`
It assumes that app is run under same user and there is a single java process PID to be listed by jps. Profiling duration is 60 seconds.
No modification of app's startup options or app restart is needed.
GUI for examining dumps is built-in into IntelliJ IDEA Ultimate: https://www.jetbrains.com/help/idea/cpu-profiler.html.