2

Possible Duplicate:
What is the garbage collector in Java?

Within my program, I have a function set up that calls another function after a certain amount of time (we'll set it as 20 minutes for purposes of this example) using ScheduledThreadPoolExecutor.

The function that gets called, we'll say is removeStat(Character char). In the method, it makes references to functions in side character (we'll say char.getClient().getWriter()). We'll assume that during the 20 minute wait period, the character in question logs out and their Character instance is no longer required.

Would the Java garbage collector remove the Character class in that time, causing for the char in removeStat to be null, or does it know to wait until the call has been made?

Community
  • 1
  • 1
PuppyKevin
  • 2,967
  • 7
  • 25
  • 27
  • lol not sure if that's sarcasm or not, but you're welcome either way ;) – jamesmortensen May 13 '12 at 00:33
  • 1
    @jmort253 - That PuppyKevin went back and accepted answers probably means it was sincere. :) – David Harkness May 13 '12 at 00:35
  • um, guys. This is not a dupe. Not of the linked question at least. This has a specific question about garbage collection and callables and scheduledthreadpoolexeutor... The linked question is a general question about the the GC. – vidstige May 13 '12 at 00:37
  • @David - I'm sure. Just I got a kick out of his response. If it was sarcasm, it would have been awesome! :) – jamesmortensen May 13 '12 at 00:37
  • @vidstige - If you think we made a mistake, you could try editing the question to make it more clear, and also make sure it differs from the [other questions about Java Garbage Collection](http://stackoverflow.com/search?q=java+garbage+collector). Sometimes mistakes are made, and questions have been reopened before after being closed prematurely or after improvements have been made. You can flag this one for reopening if there is indeed no duplicate. – jamesmortensen May 13 '12 at 00:40

1 Answers1

5

Garbage collectors (including Java's) are smart enough that you don't have to worry about this. The garbage collector won't garbage collect anything until all references to it have disappeared. If the object is still accessible, it still exists.

Neil Forrester
  • 5,101
  • 29
  • 32
  • So, lets say that instead of a 20 minute timer, I use an insane amount of time (we'll make it 7 days) - I still wouldn't have to worry about the object going null? – PuppyKevin May 13 '12 at 00:31
  • That's correct. If you can still get at it, it's still there. It doesn't pay attention to how often you access an object. It pays attention to if it is still possible to access it. Anything else risks breaking somebody's code (like yours). – Neil Forrester May 13 '12 at 00:33
  • But you may have to worry about running out of memory from all those unused but still referenced characters. – David Harkness May 13 '12 at 00:36
  • @NeilForrester - it is not just "smart enough". It is required by the JLS to behave this way. Any hypothetical GC that discards objects which have strongly reachable references is violating the spec. – Stephen C May 13 '12 at 03:39