A lot of people think there is no memory leak in Java because Garbage Collection. Some of them don’e even know there is possibility to memory leak.
In C/C++, it means you don’t free
the memory what you malloc
. But what is mean of memory leak in Java. It means garbage collector doesn’t know garbage object you don’t want to use anymore. In other word, garbage object is still been hold from other object.
For example, this is what mentioned in Effective Java, 2nd.
In this snippet of source code, pop()
will cause memory leak. This is because of the element you pop will keep in the array of elements
. You have to null original location of array.
Like this:
Other examples are you don’t close SQL Statements, SQL ResultSet, and SQL Connection or other types of connection correctly.