Photo by Caspar Camille Rubin on Unsplash

Yourkit Remote Java Application Profiling with Docker

Navnath Chinchore
Geek Culture
Published in
4 min readAug 19, 2021

--

Profiling java application always has been tricky for me. Profiling local java application is easy comparing with profiling remote java application.

Few days back I was searching ways to profile remote java application and have taken quite time to figure out how to do that without additional extra cost to a company. While searching for a solution I have tried my hands on Yourkit tool that lets you download and use it for free for 15 days period for tool evaluation purpose (I think 15 days are sufficient to identify problem with your applications 😆). So let’s get to the point without wasting any more time of yours.

Steps to follow.

Once you download Yourkit tool and extracted content then next step is to identify your java application process id and attaching it to Yourkit profiler agent. To do that run below command

sh YourKit-JavaProfiler-2021.3/bin/attach.sh
This command will list out all the java process ids running inside your docker container and will ask you to select the process id that you want to attach to profiler agent. Output of this command will look like this

Enumerating running JVMs with /home/YourKit-JavaProfiler-2021.3/jre64
[YourKit Java Profiler 2021.3-b231] Log file: /root/.yjp/log/profiler-ui-3035.log
Running JVMs:
Name | PID| Profiler Agent
-------------------------------- |------|--------------------------------
MyApplicationTwo | 82| Not loaded
Myapplication-19.3-fat. | 13| Not loaded
Enter PID of the application you want to attach (0 to exit) and press Enter:

Now enter the Process Id that you wanted to attached to profiler agent. Once you enter the process id and hit enter then it will ask you for few more inputs but I recommend you to go with the default option and just hit enter.

Then Yourkit profiler agent will start listing on localhost:10001.

Below are the list of commands that you can use with the Yourkit profiler.

Usage: 
java -jar yjp-controller-api-redist.jar <host> <port> <command>
where <command> is one of:status
capture-memory-snapshot
capture-hprof-snapshot
capture-performance-snapshot
start-sampling
start-async-sampling-cpu
start-tracing
start-call-counting
stop-cpu-profiling
clear-cpu-data
start-alloc-recording-all [alloc-sampled]
// record all objects
start-alloc-recording-adaptive [alloc-sampled]
// record all objects with size >= 4 KB, and only each 10th smaller object
start-alloc-recording=<N>,<B> [alloc-sampled]
// record all objects with size >= <B> bytes, and only each <N>-th smaller object
stop-alloc-recording
clear-alloc-data
print-alloc-object-count
// get total count of all objects created during object allocation recording
print-alloc-object-size
// get total size in bytes of all objects created during object allocation recording in full mode
start-monitor-profiling
stop-monitor-profiling
clear-monitor-data
start-stack-telemetry
stop-stack-telemetry
force-gc
clear-charts
Examples:
java -jar yjp-controller-api-redist.jar localhost 10001 capture-memory-snapshot
java -jar yjp-controller-api-redist.jar localhost 10001 start-sampling
java -jar yjp-controller-api-redist.jar localhost 10001 capture-performance-snapshot

Now Next Step is to Run your use case that you think has problem/Issue and capture snapshot to analyse with Yourkit application.

Run your use case and Capture Snapshot, Command to capture snapshot is
java -jar YourKit-JavaProfiler-2021.3/lib/yjp-controller-api-redist.jar localhost 10001 capture-performance-snapshot

Your kit store the snapshot in folder /root/Snapshots/. You will also get the information about where Yourkit has Stored snapshot after Yourkit has finished taking snapshot in below format.

 Snapshot captured: /root/Snapshots/MyApplicationOne-19.3-fat-2021-08-18.snapshot

Now you have the snapshot ready with you and you need to analyse it with the Yourkit application. To do so you need to download snapshot at your local machine and import it into Yourkit application.

Follow these steps to download snapshot at your local machine. Please note “.” (Period) at the end of commands this will copy snapshot at currant directory.

1) Copy Snapshot to your docker host
docker cp <container_id>:/root/Snapshots/MyApplicationOne-19.3-fat-2021–08–18.snapshot .

2) Copy Snapshot from docker Host to local machine
scp <Host IP>:/home/nchinchore/MyApplicationOne-19.3-fat-2021–08–18.snapshot .

Now you have Snapshot at local machine and now you will need Yourkit application to analyse snapshot. Download it from here according to your Operating system.

Once you open Yourkit profiler it will ask you for activation key. You need to fill evaluation form and you will get the evaluation key on provided mail for 15 days of free usage.

After getting evaluation key add that key and import the snapshot by clicking on Open snapshot… And start analysing.

You would likely to preform these steps repetitive un till you identify issue.

1) java -jar YourKit-JavaProfiler-2021.3/lib/yjp-controller-api-redist.jar localhost 10001 clear-cpu-data2) Run your use case 3) java -jar YourKit-JavaProfiler-2021.3/lib/yjp-controller-api-redist.jar localhost 10001 capture-memory-snapshot4) java -jar YourKit-JavaProfiler-2021.3/lib/yjp-controller-api-redist.jar localhost 10001 capture-performance-snapshot

Happy Debugging ….😆!!

--

--

Navnath Chinchore
Geek Culture

Sr Software Engineer | Talks About coding | Knowledge Sponge | Growing at Home | Trying hard to learn new Technology.