DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Optimizing Java Applications for Arm64 in the Cloud
  • JDK 17 Memory Bloat in Containers: A Post-Mortem
  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  • Comparing the Efficiency of a Spring Boot Project to a Go Project

Trending

  • From APIs to Actions: Rethinking Back-End Design for Agents
  • Tactical Domain-Driven Design: Bringing Strategy to Code
  • Top JavaScript/TypeScript Gen AI Frameworks for 2026
  • The Network Attach Problem Nobody Warns You About
  1. DZone
  2. Coding
  3. Java
  4. Diagnose CPU Spikes in a Non-Intrusive Manner

Diagnose CPU Spikes in a Non-Intrusive Manner

In this post, we are going to discuss a non-intrusive approach (i.e., approach that doesn’t add any noticeable overhead to the application) to diagnose CPU spike.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Aug. 30, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.1K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, we are going to discuss a non-intrusive approach (i.e., an approach that doesn’t add any noticeable overhead to the application) to diagnose CPU spikes. Thus, you can use this approach in your production environment to troubleshoot CPU spikes.

Works on all JVM languages:

This approach can be used to troubleshoot CPU spikes in all programming languages that run on Java Virtual Machine (JVM) like Java, Scala, Kotlin, JRuby, Jython, etc...

Step 1: Capture 360° Data

You can use the open-source yCrash data script to capture 360° data from your application stack. This script basically captures 16 different artifacts from your application stack (GC Log, thread dump, heap substitute, netstat, iostat...) and runs less than 30 seconds. Thus, it doesn’t add any measurable overhead to your application. You can trigger this script from any platform (all Linux flavors, Windows...) and any environment (bare metal, cloud, containers, k8…).

Fig: 360-degree data

Fig: 360-degree data

Here are the steps to run this script:

1. Download the latest yc-data-script 

2. Unzip the downloaded yc-agent-latest.zip file. (Say you are unzipping in ‘/opt/workspace/yc-agent-latest’ folder)

3. In the unzipped folder, you will find yc-data-script by operating system:

a) linux/yc – If you are running on Unix/Linux, then use this script.

b) windows/yc.exe – If you are running on Windows, then use this script.

c) mac/yc – If you are running on MAC, then use this script.

4. You can execute the yc script by issuing the following command:

./yc -j {JAVA_HOME} -onlyCapture -p {PID}


Where, JAVA_HOME is the home directory where JDK is installed, and PID is the target JVM’s process ID.

Example:

./yc -j /usr/java/jdk1.8.0_141 -onlyCapture -p 15326


When you pass the above arguments, yc-data-script will capture all the application-level and system-level artifacts/logs from your application stack for analysis. Captured artifacts will be compressed into a zip file and stored in the current directory where the above command was executed. The zip file will have the name in the format: ‘yc-YYYY-MM-DDTHH-mm-ss.zip‘. Example: ‘yc-2021-03-06T14-02-42.zip‘.

2. Analyze Captured Data

Once you have captured the data, you can analyze them using the yCrash server. You can upload the captured zip file to the yCrash server for analysis. The yCrash server analyzes all the captured data and generates one unified root cause analysis report instantly. Note: There is a free tier in the yCrash application, which you can use for CPU diagnosis purposes. In the yCrash incident report, you will see a ‘CPU consumption by thread’ section under the ‘Thread’ report (as shown below):

Fig: CPU consumption by threads reported by yCrash

Fig: CPU consumption by threads reported by yCrash

This section will show all the CPU-consuming threads and the exact lines of code they are working on. Equipped with this information, you can spot the ‘black sheep’ lines of code that are causing the CPU to spike up.

How Does It Work?

‘Thread dump’ and ‘top -H -p {PROCESS_ID}’ are the two artifacts that yCrash data script captures. Here ‘top -H -p {PROCESS_ID}’ command shows the list of thread Ids and the amount of CPU and memory it consumes within the specified PROCESS_ID. ‘Thread dump’ shows the code path in which threads are executing. yCrash tool marries these two data and produces the above report. For more details, refer to this post.

I hope this approach will help you to isolate CPU-consuming lines of code effectively. Happy Troubleshooting!!

Java Development Kit Java virtual machine Spike (software development) Virtual Machine

Published at DZone with permission of Ram Lakshmanan. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Optimizing Java Applications for Arm64 in the Cloud
  • JDK 17 Memory Bloat in Containers: A Post-Mortem
  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  • Comparing the Efficiency of a Spring Boot Project to a Go Project

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook