-1

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: https://en.wikipedia.org/wiki/Memory_leak#A_simple_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?

Chris Smith
  • 2,684
  • 2
  • 20
  • 46
  • 2
    This post will probably cover all the bases for you - http://stackoverflow.com/questions/4987357/can-there-be-memory-leak-in-java – jacks Oct 21 '16 at 20:36
  • There are plenty of subtle cases where an object (especially an inner class) can hold onto state that's not necessary anymore. – Louis Wasserman Oct 21 '16 at 20:37
  • Possible duplicate of [Creating a memory leak with Java](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java) – Tom Oct 21 '16 at 20:37
  • `not enough memory altogether`, well, if you had an infinite amount of memory you wouldn't care about a leak – svarog Oct 21 '16 at 20:38

2 Answers2

1

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: http://www.w3resource.com/java-tutorial/garbage-collection-in-java.php

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

Community
  • 1
  • 1
joseph
  • 1,382
  • 13
  • 28
0

When you saying something like "...I do not see any indication that Java is actually leaking memory, just there is programmer error...", you can say the same on missing memory deallocation in C. But, it is a memory leaking.

And as mentioned, in Java context, memory leaking is a situation where some objects are not used by the application any more, but Garbage Collector (GC) fails to recognize them as unused and free them.

For example, forgetting to close an opened stream:

class MemoryLeak {

    private void startLeaking() throws IOException {
        StringBuilder input = new StringBuilder();
        URLConnection conn = new URL("www.example.com/file.txt").openConnection();

        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));        

        while (br.readLine() != null) {
            input.append(br.readLine());
        }
    }

    public static void main(String[] args) throws IOException {
        MemoryLeak ml = new MemoryLeak();
        ml.startLeaking();
    }
}

Taken from: https://github.com/stas-slu/memory-leak-with-java/tree/master/src/com/memoryleak

Johnny
  • 10,849
  • 11
  • 61
  • 105