Can C# GC move memory objects

Yes it will.

But you can use the fixed keyword to stop the GC from moving it if you so desire.


Yes. The memory address of tab can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4.

The point is, in C# you don't need to bother about memory addresses as it's a managed language. All references to tab variable will be changed accordingly, and your program will survive garbage collection seamlessly.


It is certain that the array object could be moved in memory.

But note that you cannot obtain the pointer of a managed reference object, or you use a fixed block, it cannot be moved by GC within it.

If the array object is moved, the reference from variable tab to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.

Tags:

C#