This question is a continuation of this one but asking for a more specific scenario.
Lets say we have the following class:
public class Person {
private Foot left, right;
public Person(Foot left, Foot right) {
this.left = left;
this.right = right;
}
}
I was wondering if the following class would be able to be optimised from the GC's perspective if we turned it into the following:
public class Person {
private final Foot left, right;
public Person(Foot left, Foot right) {
this.left = left;
this.right = right;
}
}
If I was looking at this class I can immediately tell that the left and right variables can never be set to null early. That means that the only time the GC will need to collect the left and right objects (and decrease the references to it) for this class will be when the references to the parent Person class reach zero. It should also mean that it might be able to collect person at the same time as it collects left and right Foot objects; also resulting in less runs and a speedup.
Therefore, in this example, does marking the private member variables final mean that the code will result even a minor speedup in garbage collection (or could it possibly be used as a speedup point)?