Resource leak is generally an erroneous pattern of resource consumption where a program does not release the resource it has acquired. This can lead to poor services.
Garbage collection can only manage memory, not other system resources. If your Java program has plenty of free memory, garbage collection will not be triggered automatically.
All OSes have limits on the number of sockets, file handles, etc., that can be open. Thus, the unintentional maintenence of references to non-memory resources can lead to a resource leak. So it is extremely important to manage non-memory resources.
Classes which utilize non-memory resources should provide ways to explicitly allocate/deallocate those resources. We need to explictly call close()
methods for deallocation of file descriptors in finally{}
, as it will execute whether or not an exception is thrown.