Table of Contents
A few months into my first job as a Java developer, I was assigned to work on an application that supported the core business of my company. The application’s load was extensive, and the primary KPI was that the response time should be less than 100ms.
Although I was initially impressed by how the code met all the requirements with minimal lines, upon further investigation, it turned out that a significant portion of the business logic was outside the application itself.
Developers used stored procedures for that purpose. The reason was the belief that Java was not fast enough to perform the required operations. As it happened in its early years, Java experienced performance issues that detracted from its overall value. Implementation of key improvements such as the just-in-time compiler and generational garbage collector effectively addressed these concerns and resulted in significant performance enhancements.
This blog will cover the best practices for implementing responsive and high-performance applications.
First, let’s get familiar with a few fundamentals.
Understanding Java Memory Architecture
The Java Virtual Machine (JVM) memory architecture comprises two primary components: the Java Heap Space and the Java Stack Memory. Those two are like two sides of the same coin when it comes to managing memory in Java.
Heap Memory
Imagine heap memory as a giant warehouse filled with shelves. Each shelf holds various items, which represent the objects created by the Java program during its execution. Objects in heap memory can vary in size and complexity, from big datasets and complex objects to simple POJO instances.
Every thread operating within the JVM environment has access to this shared warehouse and can retrieve and manipulate objects within it.
In a physical warehouse, items might gather dust indefinitely. In contrast, heap memory operates efficiently. Objects reside in heap memory only as long as they’re required. Once an object is no longer needed, Java’s garbage collector sweeps through the heap, identifying and reclaiming the space occupied by unused objects.
Stack Memory
While heap memory can be expansive, stack memory operates with a more limited capacity. It is optimized for rapid access and deals primarily with primitive data types, local variables, and references to objects rather than storing the objects themselves.
Threads within the JVM environment each have their own stack, ensuring thread safety and preventing interference between concurrent method calls.
Stack memory management is relatively straightforward compared to heap memory. Each time we call a method or function, a new stack frame is created to handle all the local variables and parameters for that specific task. As methods complete their execution, their stack frame disappears, freeing up space for new method calls. This continuous cycle of allocation and deallocation keeps the stack memory tidy and efficient.
Maximizing Code Performance with Clean Code Practices
Writing clean code is considered one of those best practices mentioned earlier that not only enhances code readability and maintainability but also has a significant impact on the application’s performance.
It’s crucial to keep our stack memory fast and efficient, alleviate stress from the garbage collector, and prevent the heap from running out of memory. I will outline a few clean code guidelines that should be top of mind to achieve these goals.
JIT Compiler
Just-in-Time (JIT) compiler, at its core, is a dynamic optimization engine embedded within the JVM that should help us keep our code performant. Unlike traditional ahead-of-time (AOT) compilers that translate entire programs into machine code before execution, the JIT compiler operates on a just-in-time basis, analyzing and optimizing code segments as they are about to be executed.
When a Java program is executed, the JVM’s JIT compiler dynamically analyzes the bytecode generated by the Java compiler to identify performance bottlenecks and hotspots. The JIT compiler then applies a series of optimization techniques to the bytecode to improve performance:
- Method Inlining: This process aims to replace a method call with the actual code of the method. Instead of jumping to a separate method location in memory, executing the method, and then returning to the original location, the compiler directly inserts the method’s code at the call point. This eliminates the overhead of the method call and return operations.
- Loop Unrolling: This technique involves expanding the loop’s body to reduce the overhead associated with the loop control code, such as incrementing the loop counter and checking the loop condition. By increasing the number of operations performed per iteration, loop unrolling decreases the number of iterations and reduces the overall loop overhead.
- Constant Folding: Evaluating constant expressions during compile time instead of at runtime is the key here. For example, if your code contains an expression like 2 + 2, the compiler will replace it with 4 because the result is known and doesn’t change. This eliminates the need for the program to perform this calculation every time it runs, reducing the number of instructions executed.
- Dead Code Elimination: JIT identifies and removes code segments that are never executed or do not affect the program’s outcome. Such unnecessary code, known as “dead code,” can arise from programming errors, redundant calculations, or conditions that are always true or false. By eliminating dead code, the compiled code size is reduced.
Java developers have dedicated much of their time and effort to ensure that Java is a highly performant programming language. By adhering to best practices, it is now in our hands not to downgrade it.
Procedural Code
When writing procedural code, it’s important to consider its impact on both developers and the underlying system. Procedural code can make the system harder to maintain and optimize, as it can introduce complexity.
Additionally, local variables within procedural code can remain in stack memory for longer than necessary, leading to memory inefficiencies. This can be challenging for the JIT (just-in-time) compiler, which may struggle to optimize code effectively, resulting in performance bottlenecks. In some cases, the code might remain untouched by JIT compiler.
Using Primitives
Another important factor to consider is the use of primitives. Primitives like int and double are fundamental data types in Java and are stored directly in stack memory. This differs from wrapper classes like Integer and Double, which are objects stored in the heap.
By using primitives, you can minimize memory overhead and improve performance, as stack memory operations are faster and more efficient than heap memory operations.
Flow Control
Conditional logic, such as if-else statements, plays an important role in program flow control. However, complex conditional logic can affect performance, as the JVM must evaluate each condition sequentially.
To optimize performance, it’s best to keep conditional logic simple and leverage short-circuit evaluation where possible. This allows the JVM to bypass unnecessary evaluations, improving overall efficiency.
Loop Optimization
Shifting the focus to loop optimization, the choice between for-each loops and traditional for loops can impact performance.
For-each loops offer a cleaner syntax and can be more efficient in certain scenarios, particularly when iterating over collections. Additionally, minimizing method calls and object creation within loop bodies can further improve performance by reducing overhead.
Understanding Java Collections: Performance Matters
When I joined Leapwise, along with the hardware on my desk, I found a detailed paper explaining the nuances of choosing the right collection in Java.
Before that, my collection choices were rather limited – relying predominantly on ArrayLists, HashSets, and HashMaps, selected based solely on how my data should be organized. This paper emphasized the importance of considering performance when selecting a collection type.
JRebel wrote the collection cheat sheet that served as a guide, so make sure to check it out.
The choice of data structures from Java’s Collections Framework can have a significant impact on performance. Each data structure has its own strengths and weaknesses, so it’s important to choose wisely based on factors such as the size and type of data being stored, the frequency of data access and modification, and memory considerations.
Here I will briefly touch on the mathematical notation called Big O which, simply put, shows us how fast an algorithm’s performance changes as the amount of data it works with increases.
The Basics of Big O Notation
In Big O notation, there are several common functions used to describe the performance of algorithms, but, four of them are enough to describe collections performance – constant, logarithmic, linear, and squared functions.
Constant Time Operations in Java Collections
In the world of Java collections, operations that execute in constant time perform consistently regardless of the collection’s size. For instance, accessing an element by index in an ArrayList or appending an element to the end of it typically takes constant time.
Linear Time Operations in Java Collections
Linear time operations, on the other hand, see their execution time increase proportionally with the collection’s size. For example, iterating over all elements in an ArrayList to locate a specific element or adding/removing an element from the middle of the ArrayList both require linear time due to the need to shift elements.
Logarithmic Time Operations: TreeSet and TreeMap
Logarithmic function is relevant for collections like TreeSet or TreeMap. In these collections, operations like searching or adding elements maintain a sorted order, and as a result, their execution time increases logarithmically with the size of the collection.
Avoiding Squared Time Complexity in Java Programming
Squared time complexity should be avoided in efficient Java programming. For instance, performing a nested loop operation over all elements in an ArrayList, where each element is compared to every other element, results in quadratic time complexity, which can lead to significant performance issues.
Making Informed Choices
Given the considerations of Big O notation and the collection cheat sheet, I often evaluate my options before selecting a collection type.
Let’s envision a scenario where we must insert a substantial number of values into a collection while allowing duplicates. Initially, ArrayList seems the obvious choice. However, with the added requirement of potentially inserting values at either end and occasionally deleting some values, we may need to reassess our decision.
This scenario highlights a write-intensive application where efficient handling of frequent insertions and deletions is crucial.
Evaluating Alternatives: ArrayList vs. ArrayDeque
ArrayList‘s underlying data structure is an array. During insertion, if the array capacity is exceeded, a larger array is created, and elements are copied over. Additionally, inserting at the beginning (index 0) poses a challenge due to the need for shifting all elements.
These limitations prompt a reevaluation of our choice. ArrayDeque emerges as a more suitable alternative. Its resizing mechanism is more optimized compared to one of the ArrayList, and adding elements at the beginning aligns with its inherent design.
It’s worth mentioning that collection cheat sheets in scenarios similar to this one advocate for the usage of LinkedList. I’ve come across a tweet from the very author of LinkedList, Joshua Bloch, where he wrote, “Does anyone actually use LinkedList? I wrote it, and I never use it.” This underscores the practicality of opting for ArrayDeque in cases like the one described.
The following code evaluates the efficiency of inserting 1,000,000 elements into ArrayList and ArrayDeque collections in Java. The goal is to compare insertion times based on the elements parity, with even elements added to the beginning of the collection.
Performance Results
The insertion times on my system highlight a significant difference:
ArrayDeque: Approximately 0.2 secondsArrayList: Approximately 27 seconds
These results underscore the substantial performance advantage of ArrayDeque over ArrayList for this specific task. Feel free to run this test yourself to verify these results.
package co.leapwise.performance;
import org.junit.Test;
import java.util.ArrayList;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.IntStream;
public class CollectionPerformanceTest {
private static final int NUMBER_OF_ELEMENTS= 1_000_000;
@Test
public void arrayListInsertionPerformance() {
final List<Integer> arrayList = new ArrayList<>(NUMBER_OF_ELEMENTS);
final long arrayListTime =measureInsertionTime(arrayList, CollectionPerformanceTest::insertElements);
System.out.println("ArrayList insertion time: " + arrayListTime + " nanoseconds");
}
@Test
public void arrayDequeInsertionPerformance() {
final Deque<Integer> arrayDeque = new ArrayDeque<>(NUMBER_OF_ELEMENTS);
final long arrayDequeTime =measureInsertionTime(arrayDeque, CollectionPerformanceTest::insertElements);
System.out.println("ArrayDeque insertion time: " + arrayDequeTime + " nanoseconds");
}
private static <T extends Collection<Integer>> long measureInsertionTime(final T collection, final Consumer<T> insertionLogic) {
final long startTime = System.nanoTime();
insertionLogic.accept(collection);
final long endTime = System.nanoTime();
return endTime - startTime;
}
private static void insertElements(final List<Integer> list) {
IntStream.range(0,NUMBER_OF_ELEMENTS)
.forEach(i -> {
if (isEven(i)) {
list.add(0, i);
} else {
list.add(i);
}
});
}
private static void insertElements(final Deque<Integer> deque) {
IntStream.range(0,NUMBER_OF_ELEMENTS)
.forEach(i -> {
if (isEven(i)) {
deque.addFirst(i);
} else {
deque.addLast(i);
}
});
}
private static boolean isEven(final int i) {
return i % 2 == 0;
}
}
Conclusion
This blog only scratches the surface when it comes to Java memory and performance. It aims to keep these considerations in mind as you write your next line of code, ensuring that you are aware of how your choices impact your application’s efficiency.
If you’ve ever thought about switching from Java due to performance issues, think again. Java has evolved significantly, and with the right practices, it can deliver excellent performance. The problem likely lies elsewhere, and it’s up to you to pinpoint and resolve it. The techniques discussed here are just the beginning.
One of the upcoming posts in the Leapwise blog series will delve deeper into memory and performance, offering a more comprehensive understanding and practical insights that can enhance your Java applications. Key topics such as garbage collection, profiling, and JVM configuration play critical roles in optimizing application performance.
Stay tuned!
In the meantime, you can explore related topics on improving application performance through other Leapwise blogs, such as Caching: From In-Memory Simplicity to Distributed Complexity and 5 Useful Concepts To Consider if Sequential Processing is Not an Option.