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

  • Java: How Object Reuse Can Reduce Latency and Improve Performance
  • Performance Engineering Management: A Quick Guide
  • Pros and Cons for Using GraalVM Native-Images
  • Principles to Handle Thousands of Connections in Java Using Netty

Trending

  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • The Role of Functional Programming in Modern Software Development
  • Teradata Performance and Skew Prevention Tips
  • A Guide to Container Runtimes
  1. DZone
  2. Coding
  3. Languages
  4. Performance Tuning Java Applications in Linux

Performance Tuning Java Applications in Linux

Tips and tricks for tuning your Java applications using Linux. Including things like heap configuration and garbage collection.

By 
Sree Panchajanyam D user avatar
Sree Panchajanyam D
·
Dec. 04, 19 · Analysis
Likes (4)
Comment
Save
Tweet
Share
13.4K Views

Join the DZone community and get the full member experience.

Join For Free

top of a guitar

Learn how to make your Java applications performance perfectly.
You may also like: How to Properly Plan JVM Performance Tuning

While Performance Tuning an application both Code and Hardware running the code should be accounted for. In this blog post, we shall go over various aspects that have to be taken care of to extract maximum performance out of a Java Application running on Linux.

Thread Contention

  1. Reduce the amount of code in critical sections.
  2. Prefer synchronized blocks over synchronized methods.
  3. Prefer locks over synchronized blocks.
  4. Keep a tab on the order in which you lock resources. You could end up in a deadlock
  5. Segregate low concurrency, medium concurrency, and high concurrency use cases. Treat them separately.
  6. Use Compare-And-Swap operations for low concurrency and medium concurrency settings when possible.
  7. Do not call third party web services or execute other long-running methods in the synchronized blocks or under locks.
  8. Prefer higher-level thread constructs like locks, CountDownLatches, CyclicBarriers when compared to wait, notify and notifyAll mechanisms.
  9. Make your long-running threads interruptible. From within a thread periodically check for “interrupted” condition.
  10. Resort to ReadWriteLocks when the number of readers is much greater than the number of writers. It improves read concurrency.
  11. Use concurrent data structures only in situations where you have multiple threads accessing the data structure.
  12. Don't spray mutable state over the entire application. Restrict mutable state and deal consciously with it. This reduces the number of locks and various thread contention issues.
  13. Lookout for spinlocks like the below. They will hog your CPU. while(true) {//do something without sleeping}
  14. Always introduce a reasonable amount of sleep in such infinite while loops. Polling threads are an example where you might want to do this.

Heap Configuration and Garbage Collection

  1. Follow Standard practices of using StringBuilder for log messages.
  2. Profile your application for appropriate heap and garbage collection settings.
  3. Bad GC settings can hog your CPU, resulting in long application pauses, crash your application with an Out of Memory or freeze your application with Concurrent Mode Failures.
  4. For low latency, applications use Concurrent Mark and Sweep Algorithm — CMS or G1 GC.

Avoid Swapping to Disk

Ensure there is enough RAM to hold your java process. Swapping java process to disk is a performance killer. If your application is swapped to disk Garbage Collection cycles would be much longer as the objects have to be read from disk during GC.

Disk

  1. Use async loggers for logging.
  2. Employ context logging where applicable. Context logging is storing the logs/events of an API call in a context object and logging the context data once via async loggers. This would dramatically improve the performance of the java application and makes it easier to troubleshoot.
  3. Prefer Databases to local disk writes for any kind of persistence. Databases are optimized for disk reads and write.

CPU

  1. Use thread pools. Though threads are lightweight it is still costly to create them.
  2. Indiscriminate spawning of threads would lead to shooting up in context switches and load average which after a threshold makes the application and machine unresponsive.
  3. Along with CPU usage, lookout for Load Averages and Context Switches. They provide you the complete story about performance.

Keep an Eye on the Network

  1. Lookout for packet drops on your network.
  2. A packet drop can mean your application is too busy to receive the packet or your network is congested.
  3. Tune your network buffer sizes if need be.

Use Caching

  1. Cache frequently used data to reduce DB hits.
  2. Tune your Cache Configuration (eviction, expiry, size, consistency, concurrency) for each use case separately.
  3. Separate read-intensive caches from writing-intensive caches for better locking.
  4. Use distributed caching judiciously. Inappropriate use of Distributed caching introduces new problems instead of solving existing ones.

Communication

  1. For Near, Real-Time communication prefer asynchronous communication.
  2. Have timeouts for outgoing API/RMI calls set so that one long-running operation does not bring down the entire ecosystem.

Profile and Document the Changes

Always profile and document your changes. Document in a lucid manner why a particular decision is being made. Significant decisions today may lose their relevance tomorrow. When this happens future engineers on your application should have enough information to revert or extend them. This is how applications survive. Nobody wants to sit on a ticking time bomb not knowing when it would explode.

Profiling Tools

  1. Jstack for collecting thread dumps.
  2. Jmap for collecting heap dumps and live object counts.
  3. Jvisualvm/Jconsole — analyze heap, threads, collect thread dumps - available by default in JDK.
  4. Eclipse Memory Analyzer (MAT) — for analyzing the heap dumps.
  5. Thread Dump Analyzer (TDA) — for analyzing thread dumps, detecting long-running threads and deadlocks.

Advice on Performance Tuning

Plan and budget for performance tuning early in the project but do not prematurely rush into it. This might make a mess out of your core business logic. One of the applications I know was eagerly optimized to restrict the number of records fetched from the DB and this was not documented.

There were all sorts of issues and this piece of code was buried so deep that it took a considerable amount of time to figure this out. Of course, if you have encountered and solved the same business problem multiple times this advice does not apply.


Further Reading

How to Improve the Performance of a Java Application

How to Solve Your Java Performance Problems (Part 1)

application Java (programming language) Database garbage collection Threading Linux (operating system)

Opinions expressed by DZone contributors are their own.

Related

  • Java: How Object Reuse Can Reduce Latency and Improve Performance
  • Performance Engineering Management: A Quick Guide
  • Pros and Cons for Using GraalVM Native-Images
  • Principles to Handle Thousands of Connections in Java Using Netty

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!