0

I just gone though memory leaks articles but not getting Why open connections or unclosed resources can not be garbage collected in jvm? Can someone please help with example. Thanks in advance.

  • 1
    What is saying that these resources "can not" be garbage collected? The statement sounds too strong. I think the correct statement is, "are not" or "generally are not" or "should not rely upon". – boneill Aug 23 '21 at 16:27
  • I mean, if we not closed open connections or unclosed resources after our method call, then why it is still not eligible candidate for GC – Sachin Rane Aug 24 '21 at 02:45

1 Answers1

2

The Java garbage collector isn't required to ever reclaim an object, even when it's unreachable. In practice, it's usually reclaimed eventually, but this can take hours (or days, or ∞), depending on the application and garbage collection activity. Read up on generational garbage collection to learn more about this.

The first JVM only supported a conservative garbage collector. There's a bunch of stuff online regarding this design as well, but the main caveat when using one of these type of collectors is that it might never identify an object as being unreachable even when it truly is.

Finally, there's the "epsilon" collector option, which never runs garbage collection at all. This is just used for performance testing and for short-lived Java processes. Once a process exits, the open resources get closed by the operating system anyhow.

boneill
  • 1,478
  • 1
  • 11
  • 18
  • 1
    “In practice, it always will be reclaimed eventually”—No. In practice, you don’t notice when a few small objects are never collected. Except, when they happen to encapsulate a connection or other non-memory resource that you expect to be closed by a finalizer. Then you notice. In fact, the longer an object happens to be overlooked by the garbage collector, the less likely becomes its collection. Because collectors like G1 focus on the biggest return which they get by processing the region full of dead young objects, rather than the one containing a single dangling old object. – Holger Aug 24 '21 at 08:46
  • Yes, this is what generational collection is all about. Hence my remarks at the end of the sentence fragment you quoted. I think your concern is that someone might read my answer and happily assume that one can always let the GC take care of closing resources, but that was not my intent. – boneill Aug 24 '21 at 15:10
  • 1
    I think, the intent is clear and no-one would assume after reading your answer, that using the GC for (non-memory) resource cleanup was a good idea. That’s fine. However, the statement “in practice, it always will be reclaimed eventually” is just wrong, as objects may stay uncollected for an infinite time. That applies to almost every GC algorithm currently in use in practice. Be careful with the word “always”. • There’s, by the way, another important, often overlooked point, see [this issue](https://stackoverflow.com/q/58714980/2711488) or [that](https://stackoverflow.com/q/46971788/2711488)… – Holger Aug 24 '21 at 16:31