Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Addressing Application Performances in Java Applications

DZone 's Guide to

Addressing Application Performances in Java Applications

If you're a Java developer, you might need to hear some of these tips to keep your apps performant.

· Performance Zone ·
Free Resource

While solving issues related to NFRs (especially application performance), I always think about why we as developers give less importance (honorable exceptions are always there) to performance. Is this due to a lack of awareness?

Many times, we start thinking of performance only at the time of a performance test or after deployment.

Increased velocity (speed) and volume (size) have impacted our daily lives: our mobile phones have gone from 2G to 4G, and whereas a 40-80 GB hard drive was once a luxury, USB drives now come in terabytes.

The Importance of Application Performance

For any application we deliver, must have these characteristics.

  1. Functionally correct;

  2. Performant;

  3. Look and feel: Should have well-structured, readable, reusable code, and use the latest frameworks, libraries, and tools;

  4. Test coverage: Functionality should be verified by testing tools, that the application covers all intended functionality.

I have seen that many of us are delivering applications conformant to all but performance. So why am I giving the second place of importance to performance?

Let's take this example: as an employee,

  • Every day I come to the office at 8 AM, work for 9 hours with a 30-minute lunch break and a 15-minute tea break, and leave the office around 5 pm (functionally correct);

  • I am strictly obeying all office manners, always clean-shaven, wearing work-appropriate attire, and my desk is always tidy and clean (look and feel);
  • My office attendance is verified by my daily in-out swipes, system log-in and logouts, and managers supervision (test coverage).

Now, what if I am taking ten days to complete two days' worth of work? That means I am not at all performing well. Will my organization allow me to work for them if I am not performing?

Observations

With all due respect to developers who work very hard to deliver functionally correct code, I am sharing a few of my observations.

Improper Usage of Threads

For every value of iteration, a thread is spawned and the program waits until that thread finishes, making the whole behavior sequential.

Instead, a Runnable/Callable list should be populated inside a loop and then invoke all threads at once using parallel processing.

Database Calls A Loop for Fetching Master Data, Collection of Static Values Created Iteratively.

This is like applying for a new passport for every international trip. Please load and cache all master data, constants, and configuration fields only once, when the application starts, and use them throughout the application.

Objects Like Date Format Are Created in A Loop

What's the benefit of creating a new object every time in a loop for a format like "ddMMyyyy?" Please define it as a constant and use everywhere. Java 8 has a DateTimeFormatterclass that has some predefined constants.

Creation of Timestamp Object Just for Logging Purposes in A Loop

A new TimeStamp object is created in a loop just to log current date time. Aren't all current log framework support patterns to log class, method, date time, etc.? Please use those features of log frameworks.

Network Connection Objects Like Response Are Not Closed

As the number of network requests increases, the network becomes a bottleneck and network performance degrades due to the number of open network connections. Please close connections whenever a network task completes.

And as we say in real life, every single penny counts. Don't use methods like String.valueof(int)  which in turn calls Integer.toString(int)you will save one method call by using Integer.toString(int).

Conclusion

It is true that for an application to perform well, its architecture (high and low) has to be carefully designed by considering current and future NFRs.

But there are certain areas where we as developers could definitely apply optimizations which will benefit to overall application performance. We are moving towards cloud-based architectures, where every usage of resources has a cost associated with it, so keep in mind that nothing is free.

While delivering functionality, please ask yourself, can I optimize this code further?

Topics:
java ,performance ,tips and tricks ,best practices

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}