So I have an object which needs certain variables to be instantiated. These variables are passed to the object through an array of objects. Then, each element in the array gets assigned to an internal variable.
Does this array get garbage collected after the internal variables are assigned and the array is never referenced again, or should it be manually done?
class MyObject () {
public static Object [] values;
public void setvalues(inputArray) {
values = inputArray;
}
}
Memory is kind of important because I need to create a few hundred of these objects plus the rest of the code.
Whether the array is eligible for GC depends on this condition:
If, you have something like this:
Then
myArray
is not eligible for garbage collection because the variablemyArray
in theFoo
object is still referencing it. You can then setmyArray
tonull
to make it eligible for GC.If
myArray
were a local variable, however:Then it is eligible for GC after
someMethod
returns becausemyArray
will have gone out of scope by then.Also, note that "eligible for GC" doesn't mean "will be collected immediately". It just mean that the GC has a possibility of collecting it in the future. When exactly? We don't know.