When a program allocates memory on the heap, the OS cannot automatically deallocate it, leaving the full responsibility heap management to the programmer. However, if negligence persist, the forgotten memory can pile up and cause the program to crash due to OutofMemoryErrors**.** This is called a memory leak.
While intermediate-level languages such as C and C++ require programmers to manually manage the heap, high-level languages have garbage collectors to automatically deallocate unused heap memory. An object is eligible for garbage collection if it is unreachable.
There are generally four ways to make an object unreachable:
Example
Integer i = new Integer(4);
// the new Integer object is reachable via the reference in 'i'
i = null;
// the Integer object is no longer reachable.