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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • All You Need To Know About Garbage Collection in Java
  • Difference Between InitialRAMPercentage, MinRAMPercentage, MaxRAMPercentage
  • Heap Memory In Java Applications Performance Testing
  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers

Trending

  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  • Integration Isn’t a Task — It’s an Architectural Discipline
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • A Deep Dive Into Firmware Over the Air for IoT Devices
  1. DZone
  2. Coding
  3. Java
  4. How Java Profilers Work

How Java Profilers Work

Learn more about how Java profilers work.

By 
Tim Ojo user avatar
Tim Ojo
·
May. 09, 19 · Presentation
Likes (7)
Comment
Save
Tweet
Share
14.9K Views

Join the DZone community and get the full member experience.

Join For Free

One of the best tools we have today for understanding application behavior and troubleshooting performance issues are Java profilers. Java profilers monitor JVM execution at the bytecode level and can provide information on thread execution and locks, heap memory usage, garbage collection, hot methods, exceptions, class loading, and more.

How Java Profilers Work

Profilers inspect the state of a JVM either by passively listening for events from the JVM or by actively querying the JVM for its state. Profilers can also modify the bytecode of classes to add instrumentation code, like inserting a methodEntered and methodExit event at the beginning and end of a method or inserting an objectCreated event into a constructor. 

There are lots of events that are generated by the JVM, and these can be categorized into two types: instant events and duration events. Instant events are one-time events that have a timestamp and the event data. Examples include exception events, class load events, and object allocation events. Instant events let us know that something has occurred and can be reacted to or simply watched and analyzed. Duration events have a start time and an end time and are therefore used to provide timing for some activity. Examples include garbage collection, which tells you the start and end time of garbage collection, monitor wait which tells you how long a thread waited on an object, and monitor contended, which tells you how long a thread was waiting on a lock to be released.

In addition to the generated events, the JVM also has profiling functions that return data about its internal state. Some commonly used ones are the thread functions like GetThreadState and GetAllThreads, and the stack trace functions like GetStackTrace and GetAllStackTraces. This internal state data is queried periodically by the profiler in a process known as sampling and the sampling period is how often the functions get called to fetch the data. For example, every 20 milliseconds. 

Performance Analysis Using Java Profilers

When analyzing application performance, there are a number of profiling activities that are commonly performed by programmers and analysts. These include CPU profiling, memory profiling, and thread profiling. In some cases, depending on the type of application, I/O Profiling becomes important as well, but the majority of applications are most concerned with the aforementioned 3. 

CPU profiling is primarily concerned with the frequency and length of time of method execution. With CPU profiling, you can find out what methods run the most frequently, therefore, eat up the most CPU time. These are commonly referred to as "hot methods." This gives you an indication of what methods or areas of the code you should focus on optimizing.

There are two ways in which CPU profilers get method execution information; by sampling or by instrumentation.

Sampling profilers work by periodically querying the JVM for all the running threads and getting the stack trace for each thread. It then determines what method each thread was executing when the sample was taken and compares the samples to determine how much time was spent in that method. Profiling by sampling has the least amount of overhead and is, therefore, suitable for production use. However, because it's an indirect way of measuring method execution, it can be error-prone or inaccurate depending on the execution flow of the application and how often the profiler samples. To minimize sampling errors, your options are to profile over a longer period of time, reduce the interval between samples, or both. However, reducing the interval between samples results in increasing the overhead on the JVM, therefore, there's a trade-off that has to be made between accuracy and overhead.

Instrumenting profilers work by modifying the application's bytecode and inserting code for counting invocations or timing methods. This makes them more accurate in determining method invocation counts and potentially more accurate in determining method timing. However, instrumenting application bytecode has a higher potential for introducing performance differences for two reasons. One is that the instrumentation code will likely have some overhead attached to it and the other is that; depending on how the instrumentation is done there may be some optimizations that could have been applied to the non-instrumented code that can't be applied to the instrumented code. 

The next common profiling activity is memory profiling. Memory profiling is primarily concerned with understanding what objects are using up memory and how memory is being freed by garbage collection. With memory profiling, you can monitor the memory usage of your class objects over a period of time, find out what objects are growing and shrinking in size, and where in your code these allocations are taking place. You can also analyze garbage collections. You can see the number and types of garbage collections that have occurred, the length of the pause times for each garbage collection, and how much memory they were able to free.

The third common profiling activity is thread profiling. Thread profiling is primarily concerned with understanding what states threads are in and why. With thread profiling, you can see if your threads are able to run in parallel or not, find out what how much time your threads spend sleeping, waiting, blocked, or executing, and find and analyze cases of high lock contention. Thread profiling is useful when your application is not as performant as it should be yet its CPU usage is low. In this case, your application performance could be hindered by locking, and thread profiling helps you find that out.

There are lots of different Java profilers available today. The screenshots in this article are from Java Flight Recorder, a Java profiler developed, and recently open-sourced by Oracle. To get started with Java Flight Recorder, you can check out this Youtube video from Oracle.

For more information on how to tune your Java application for optimal performance, including an example where I use a profiler on a Spring application, discover some hidden performance flaws, and make some tweaks, check out my Pluralsight course: Java Performance Tuning. In there, I cover tools and techniques for analyzing and troubleshooting Java performance issues, JVM tuning topics, Java profiling, and best practices for writing high-performance application code.

Java (programming language) application Event Memory (storage engine) Java virtual machine

Published at DZone with permission of Tim Ojo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • All You Need To Know About Garbage Collection in Java
  • Difference Between InitialRAMPercentage, MinRAMPercentage, MaxRAMPercentage
  • Heap Memory In Java Applications Performance Testing
  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!