13

Possible Duplicate:
Creating a memory leak with Java

There is a "Garbage Collector" in Java, but does this mean that memory leaks are totally absent in a Java applications? If not, how and why do they happen?

I am more interested in scenarios in applications using JavaSE.

Community
  • 1
  • 1
Bhushan
  • 18,329
  • 31
  • 104
  • 137

5 Answers5

17

No - memory leaks can still exists in Java. They are just of a "different kind".

Wiki: Memory Leak

A memory leak, in computer science (or leakage, in this context), occurs when a computer program consumes memory but is unable to release it [the memory] back to the operating system.

In the case of Java it (normally) is when an unused/unneeded object is never made eligible for reclamation. For instance, an object may be stashed in a global List and never removed even if the object is never accessed later. In this case the JVM won't release the object/memory - it can't - because the object might be needed later, even if it never is.

(As an aside, some objects, such as directly allocated ByteBuffers also consume "out of JVM heap" memory which might not be reclaimed in a timely manner due to the nature of finalizers and memory pressure.)

In the case of Java, a "memory leak" is a semantic issue and not so much an issue of "not being able to release under any circumstances". Of course, with buggy JNI/JNA code, all bets are off ;-)

Happy coding.

  • 2
    As a follow-up, no garbage collector can always reclaim all unused memory. It's provably impossible to build a GC that can detect whether a certain piece of memory will never be used again, so most GCs use whether the memory is **reachable** as a surrogate. – templatetypedef Jul 12 '11 at 19:23
  • thanks for quick response pst. the issue which you have mentioned is right, but its more because of (bad) programming practices, right? Suppose there are some objects in memory which are "really" eligible for reclamation, will they be certainly reclaimed by JVM? for sure? – Bhushan Jul 12 '11 at 19:23
  • 1
    @10101010 Reclaimable objects, those which are not strongly reachable from a root, **will be reclaimed "at some point"**, yes. However, in the case of objects that release "native" memory in the finalizer, it may be possible that the ("native") memory is not released fast enough -- even if it *could be* -- and thus could result in a [premature] OOM. –  Jul 12 '11 at 19:25
  • 1
    A minor quibble: in a perfect JVM, the GC will always reclaim any objects that are no longer reachable. But due to JVM bugs and limitations of the GC's ability to compute reachability, there are some ways to keep large amounts of data (ClassLoaders for example) rooted for GC purposes while still not being "reachable" in the sense of normal Java code being able to dereference and read the corresponding object values. – Daniel Pryden Jul 13 '11 at 22:13
  • I've expierienced this with a bad-implemented observer pattern. Memory leaks, definitively exist. – santiagobasulto Sep 09 '11 at 22:32
2

Depends on how you define memory leak.

If you specifically mean having allocated memory that is no longer referenced by some memory root, then no, the garbage collector will eventually clean all of those up.

If you mean generally having your memory footprint grow without bound, that is easily possible. Just have some collection referenced by a static field and constantly added to.

ILMTitan
  • 10,751
  • 3
  • 30
  • 46
1

Any object that has one or more live references to it will not be garbage-collected. So as long as some variable (either static, in the heap, or in the stack) refers to an object, that object will continue to occupy non-reclaimable memory space.

Unclosed resources (like sockets, JDBC connections, etc.) and constantly growing static collections are some of the better-known leak producers.

David R Tribble
  • 11,918
  • 5
  • 42
  • 52
1

Memory leaks in java are very possible. Here is a good article which has an example using core java. Fundamentally, a memory leak happens in java when the garbage collector cannot reclaim an object because the application holds a reference to it that it won't release, even though the object itself might no longer be used. The easiest way to create a memory leak in java is to have your application hold a reference to something, but not using it.

In the example, the unused object is a static List, and adding things to that list will eventually cause the JVM to run out of memory. Static collections are a pretty common source of "leaks", as they are typically long lived and mutable.

Paul Sanwald
  • 10,899
  • 6
  • 44
  • 59
1

There are a few good responses so far. I don't want to recreate those posts, so I'll just add that one thing most people don't think about in connection with this subject is leaks in native code running via JNI. Native code running via JNI uses the JVM's heap space to allocate memory. So, if your application uses native code running via JNI that has a leak, your application has a leak.

Paul Morie
  • 15,528
  • 9
  • 52
  • 57