Summary
In this chapter, we learned how to avoid memory leaks in our code. The first step was to understand that memory leaks occur when objects, when no longer needed, maintain links to the stack. This prevents the garbage collector from reclaiming them. Given that memory is a finite resource, this is never desirable. As these objects accumulate, your application slows down and eventually crashes.
One common source of memory leaks is bugs in our code. However, there are ways to debug memory leaks. In order to demonstrate how to debug leaky code, we presented a program containing a memory leak. VisualVM is a tool that enables us to monitor the metrics of interest—the heap memory footprint, garbage collection activity, and the heap dump (when we run out of heap space).
The heap footprint validated the presence of a memory leak as it showed the used heap space totally occupying the available heap space. In other words, objects on the heap were not reclaimed. Meanwhile, the...