I have done some research on the java garbage collector and understand that an object who is no longer referenced will/should be handled by the garbage collector. In terms of arrays-of-objects, I am aware that assigning a new object to a location in the array does not properly deallocate the previously allocated object.
Setting an object in an array to null
or to another objects makes it eligible for garbage collection, assuming that there are no references to the same object stored anywhere.
So if you have
Object[] array = new Object[5];
Object object = new Object() // 1 reference
array[3] = object; // 2 references
array[1] = object; // 3 references
object = null; // 2 references
array[1] = null; // 1 references
array[3] = new Object(); // 0 references -> eligible for garbage collection
array = null; // now even the array is eligible for garbage collection
// all the objects stored are eligible too at this point if they're not
// referenced anywhere else
The garbage collection rarely will reclaim memory of locals though, so garbage collection will mostly happen already outside of the scope of the function.