I'm working on a dueling system for a game that I've been working on, here's the skeleton of the class:
public class Duel {
private Champion challenger;
private Champion defendant;
private boolean duelStarted;
private long timer;
public Duel(Champion challenger, Champion defendant) {..
public void tick() {..
public void declineDuel() {..
public void endDuel(ResultType resultType, Champion winner, Champion forfeit) {..
public void declareWinner(Champion player) {..
public void declareForfeit(Champion player) {..
private enum ResultType {..
public Champion getChallenger() {..
public Champion getDefendant() {..
}
This class will be assigned to the Champion's class for both the challenger, and the defendant.
When a duel ends (Effectively by calling Duel#declineDuel()
or Duel#endDuel(ResultType, Champion, Champion)
or due to something like a Champion#Disconnect()
, how should I properly set the class up to be collected by the Java garbage collection.
I've never completely understood this, would I need to set everything inside of the class to null, as-well as the Duel currentDuel;
declaration in Champion.class
or should I only set the currentDuel to equal null, which will effectively stop referencing ( I would assume, this is what I'm confused about ) to this instance of the class.
I'm don't quite understand what the garbage collection would consider a "Referenced" class, would Duel#getChallenger()
being set make it be considered referenced still, even though it's not being touched from an outside class?
I guess I'm confused between in-bound and out-bound references, would anybody care to shed some enlightenment since I can't seem to understand what's going on when i rtfm.
how should I properly set the class up to be collected by the Java garbage collection.
You may not need to do anything. If there are no references to the Duel
from elsewhere, then the Duel
object itself is eligible for garbage collection... and if the Duel
is the last thing that refers to the relevant Champion
objects (etc) then those will be eligible for garbage collection too.
If Champion
refers to Duel
via a currentDuel
field (as it sounds like it does) then if you've got a separate reference to the champions (e.g. you've got a list of champions somewhere) then those champions will prevent the Duel
from being garbage collected, until currentDuel
is set to a different value. But really, you should be thinking in terms of what's logically useful rather than worrying about garbage collection... presumably once the duel is completed, it makes sense to set currentDuel
to null anyway, as the champions are no longer fighting that duel... and at that point, they won't be keeping the Duel
alive any more. It's very unlikely that you need to change anything in Duel
itself though.
If the champions themselves are eligible for garbage collection anyway, then you don't need to worry about the cycle of references between Duel
and Champion
- that won't prevent garbage collection.
The principle in all of this is the question of "Can I still reach the relevant object through any code path from any live thread?" If you can't, then it will be eligible for garbage collection. If you can reach the relevant object, then it won't be garbage collected.
See more on this question at Stackoverflow