Reputation: 492
I am manually copying a smaller array into a larger array:
T is constrained to class, new()
Why does this trigger the GC? Is the assignment to the new array not by reference? Why are the old elements of the old array still garbage collected? Does the assignment inside the first loop really copy them?
public void Resize()
{
T newArray = new T[oldArray.Length * 2];
for (int i = 0; i < oldArray.Length; i++)
{
newArray[i] = oldArray[i];
}
for (int i = oldArray.Length; i < newArray.Length; i++)
{
// Assign new elements to the new array
}
oldArray = newArray;
}
Upvotes: 0
Views: 1139
Reputation: 13207
As soon as you call new
you create a reference to a new object on the heap. When you assign the reference
oldArray = newArray;
both references point to the new object. If there are no more references to the object oldArray
pointed to, it is eligible for garbage collection.
For reference:
Releasing Memory. The garbage collector's optimizing engine determines the best time to perform a collection based on the allocations being made. When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots. Every application has a set of roots. Each root either refers to an object on the managed heap or is set to null. An application's roots include global and static object pointers, local variables and reference object parameters on a thread's stack, and CPU registers. The garbage collector has access to the list of active roots that the just-in-time (JIT) compiler and the runtime maintain. Using this list, it examines an application's roots, and in the process creates a graph that contains all the objects that are reachable from the roots. Objects that are not in the graph are unreachable from the application's roots. The garbage collector considers unreachable objects garbage and will release the memory allocated for them. During a collection, the garbage collector examines the managed heap, looking for the blocks of address space occupied by unreachable objects. As it discovers each unreachable object, it uses a memory-copying function to compact the reachable objects in memory, freeing up the blocks of address spaces allocated to unreachable objects. Once the memory for the reachable objects has been compacted, the garbage collector makes the necessary pointer corrections so that the application's roots point to the objects in their new locations. It also positions the managed heap's pointer after the last reachable object. Note that memory is compacted only if a collection discovers a significant number of unreachable objects. If all the objects in the managed heap survive a collection, then there is no need for memory compaction. To improve performance, the runtime allocates memory for large objects in a separate heap. The garbage collector automatically releases the memory for large objects. However, to avoid moving large objects in memory, this memory is not compacted.
Upvotes: 3