Can there be memory leak in Java

18,820

Solution 1

Can there be memory leak in Java?

The answer is that it depends on what kind of memory leak you are talking about.

Classic C / C++ memory leaks occur when an application neglects to free or dispose an object when they are done with it, and it leaks. Cyclic references are a sub-case of this where the application has difficulty knowing when to free / dispose, and neglects to do it as a result. Related problems are where the application uses an object after it has been freed, or attempts to free it twice. (You could call the latter problems memory leaks, or just bugs. Either way ... )

Java and other (fully1) managed languages mostly don't suffer from these problems because the GC takes care of freeing objects that are no longer reachable. (Certainly, dangling pointer and double-free problems don't exist, and cycles are not problematic as they are for C / C++ "smart pointers" and other reference count schemes.)

But in some cases GC in Java will miss objects that (from the perspective of the programmer) should be garbage collected. This happens when the GC cannot figure out that an object cannot be reached:

  • The logic / state of the program might be such that the execution paths that would use some variable cannot occur. The developer can see this as obvious, but the GC cannot be sure, and errs on the side of caution (as it is required to).
  • The programmer could be wrong about it, and the GC is avoiding what might otherwise result in a dangling reference.

(Note that the causes of memory leaks in Java can be simple, or quite subtle; see @jonathan.cone's answer for some subtle ones. The last one potentially involves external resources that you shouldn't rely on the GC to deal with anyway.)

Either way, you can have a situation where unwanted objects cannot be garbage collected, and hang around tying up memory ... a memory leak.

Then there is the problem that a Java application or library can allocate off-heap objects via native code that need to be managed manually. If the application / library is buggy or is used incorrectly, you can get a native memory leak. (For example: Android Bitmap memory leak ... noting that this problem is fixed in later versions of Android.)


1 - I'm alluding to a couple of things. Some managed languages allow you to write unmanaged code where you can create classic storage leaks. Some other managed languages (or more precisely language implementations) use reference counting rather than proper garbage collecting. A reference count-based storage manager needs something (i.e. the application) to break cycles ... or else storage leaks will ensue.

Solution 2

Yes. Memory leaks can still occur even when you have a GC. For example, you might hold on to resources such as database result sets which you must close manually.

Solution 3

Well, considering that java uses a garbage collector to collect unused objects, you can't have a dangling pointer. However, you could keep an object in scope for longer than it needs to be, which could be considered a memory leak. More on this here: http://web.archive.org/web/20120722095536/http://www.ibm.com:80/developerworks/rational/library/05/0816_GuptaPalanki/

Are you taking a test on this or something? Because that's at least an A+ right there.

Solution 4

The answer is a resounding yes, but this is generally a result of the programming model rather than an indication of some defect in the JVM. This is common when frameworks have lifecycles different of that than a running JVM. Some examples are:

  1. Reloading a context
  2. Failing to dereference observers (listeners)
  3. Forgetting to clean up resources after you're finished using them *

* - Billions of consulting dollars have been made resolving the last one

Solution 5

Yes, in the sense that your Java application can accumulate memory over time that the garbage collector is unable to free.

By maintaining references to uneeded/unwanted objects they will never fall out of scope and their memory will not be claimed back.

Share:
18,820
javaguy
Author by

javaguy

Updated on June 07, 2022

Comments

  • javaguy
    javaguy almost 2 years

    I get this question asked many times. What is a good way to answer

  • T.J. Crowder
    T.J. Crowder about 13 years
    If you're not referencing the object, it can be GC'd. If you are (and not in a closed loop), then it's not a "leak." Ipso, facto.
  • Anon.
    Anon. about 13 years
    @T.J. Crowder: That's a very ... strict interpretation of "leak". And not a very useful one, at that. Suppose some class maintains an internal cache of various objects, but keeps re-adding new instances to that cache instead of reusing them. Memory usage keeps going up and the heap is filled with objects that will never be used again, and that sure looks like a "memory leak" to me.
  • T.J. Crowder
    T.J. Crowder about 13 years
    @Anon: I was mostly being frivolous. But your point is actually quite a good answer, I'd suggest adding it as one.
  • Pacerier
    Pacerier over 12 years
    How can JVMs have memory leaks when they aren't the ones having the memory ?
  • ed209
    ed209 over 10 years
    This is not a memory leak it is just poor programming. All of these objects can still be referenced so they are not "leaked"
  • Stephen C
    Stephen C almost 4 years
    JVMs >do< allocate and use memory. Both heap and non-heap memory.