0

I am looking at the use of System.Runtime.InteropServices.MemoryMarshal.GetReference() in the dotnet runtime repository, and I don't see any explicit pinning of the obtained references being done. Which suggests that maybe the object we have obtained a ref on has been pinned, or the reference is updated when the GC moves the object.

It looks to me that the ref must be being updated, but I am not sure, and can't find any documentation on this.

Here's the class I am looking at:

ArraySortHelper.cs

redcalx
  • 8,177
  • 4
  • 56
  • 105
  • Memory is getting allocated and probably using the standard windows allocation method. So you have to make sure you deallocate the memory to avoid memory leaks. The GC in managed c# will not deallocate memory allocated using the windows allocation method. – jdweng Mar 28 '21 at 13:07
  • @jdweng The question is regarding references/pointers to existing allocated memory, e.g. a Span<> over the elements of an array on the heap. Do such references get updated when the object pointed to moves? As I type I am now thingking that this perhaps is the distinction between a 'managed ref' and a pointer (which is the result of a 'fixed' operation). – redcalx Mar 28 '21 at 14:52
  • 2
    No, the ref/Span/Memory family was designed to not require pinning. Implemented with *interior pointers*, gives the GC a major headache but the lifetime is usually short enough to not cause perf issues. – Hans Passant Mar 28 '21 at 15:06
  • do not get memory on the execution stack confused with allocated memory. Allocated memory is assinged from unused memory by the operating system. Net objects are taken out of the execution stack. The size of the stack is based on the compiler. The GC only works on the second type.and usually doesn't move. The Net managed library is using BY REFERENCE on class objects and when the object get reassigned they do not get moved. – jdweng Mar 28 '21 at 15:48

0 Answers0