Memory Leaks
Memory leaks occur as a result of improper memory management and can directly impact the performance of an application. These leaks occur when memory is improperly deallocated or when it is allocated but becomes inaccessible. Improper memory management not only negatively impacts performance but can also hinder scalability, result in system crashes due to OutOfMemoryError
, and ruin the user experience. Many developers implicitly trust Java’s garbage collector (covered in Chapter 1) to manage memory while their applications run; however, despite the garbage collector’s incredible capabilities, memory leaks are a persistent issue.
The garbage collector is not faulty; rather, memory leaks occur when the garbage collector is unable to reclaim memory that stores objects that are no longer needed by the application. Improper referencing is the primary culprit, and fortunately, we can avoid this. This chapter provides techniques, design patterns, coding examples...