Chris Smith Chris Smith - 1 year ago 101
Java Question

Java memory leaks?

My understanding is that Java does not have memory leaks. By memory leak, I mean memory that you cannot access anymore (not memory that you could, but you don't). Here is an example in C:

A friend linked me to these two articles:

However, I do not see any indication that Java is actually leaking memory, just there is programmer error such as:

  • storing too much unnecessary data (like a list that you keep appending to)

  • not enough memory altogether (and need more RAM/heap space)

  • native code doesn't manage its memory correctly

  • not closing native resources such as streams or databases

  • potentially a bug in the JVM that needs to be fixed

He also mentioned cyclic references, but I believe Java deals with these as well because of its tracing garbage collector.

Am I correct in this? Other than the issues I listed, does Java have any "true" memory leaks?

Answer Source

A memory leak in java is caused by keeping a reference into a object that you thought you deleted but you left one or more references that provides a path to the object from a root GC (for instance a static object). That means the JVM has to assume that the object might be accessed in the future. It does not know that programmer doesn't need the object anymore.

It's sort of like forgetting to free memory in C/C++. The difference here is that you forgot to remove a reference to it rather than freeing the object itself.

Read more about it here:

You can see examples of memory leaks here: Creating a memory leak with Java

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download