Lets say I have a Fragment defined as such:
public class MyFragment extends Fragment {
private static String sample = "";
public static void setSample(String s) {
sample = s;
}
}
for the lifetime of the Application, will sample get garbage collected (whether or not any references to MyFragment exist – which shouldn’t matter I think)?
You’re right that the number of instances of
MyFragmentdon’t matter.The
samplevariable will effectively be a GC root for as long as the classloader that loadedMyFragmentis alive.It’s important to note that variables are never garbage collected – objects are.