The garbage collector reclaims memory occupied by an object once it determines that object is no longer accessible. A memory leak in Java is caused when an object that is no longer needed cannot be reclaimed because another object is still referring to it.
Sorry Les but coming from a C++ background I think memory leak means a memory which has been allocated and cannot be accessed and hence cannot be reused/freed by system, but in Java that is not the case as garbage collector takes care of such cases,though it is not guaranteed to run programmer does not have to worry about it as in C++ and hence from programmers point of view there is no scope of memory leak in Java. Pls correct me if I am wrong Regds, Bhushan
There is opportunity for memory leaks in Java. Both from a JVM perspective (faulty compilers, fault garbage collector algorithms!) and as a programmer (bad code), if we're not careful. One could argue that bad garbage collectors aren't really 'leaks' as much as they are 'late to collect' or 'non-agressive'. But my point is.. memory leaks can still happen.
I did a little bit of C/C++, and of course in that language, if you are not very careful, you can 'leak' away memory quite easily.
It's much harder in Java, since there is a garbage collector. But it's not infallible.