The question is how .Net runtime understands that fields of a structure, that was placed onto memory using `Marshal.StructureToPtr`, must not be freed by GC.

Below the scenario.

I have the following structure:

```
[StructLayout(LayoutKind.Sequential)]
public struct SomeStruct
{
    public string s;
    public Stream stream;

    public SomeStruct(string s)
    {
        this.s = s;
        this.stream = new MemoryStream(0x100);
    }
}

```

There is a method that instantiates the structure and drops it to memory:

```
static IntPtr GetStructRawData()
{
    IntPtr ptr = Marshal.AllocHGlobal(1024);
    Marshal.StructureToPtr(new SomeStruct("hi"), ptr, false);
    return ptr;
}

```

Then I can make new structure from the raw memory:

```
IntPtr ptr = GetStructRawData();

GC.Collect();

SomeStruct struct2 = (SomeStruct)Marshal.PtrToStructure(ptr, typeof(SomeStruct));

```

Aftet that `struct2` really contains correct string ("hi"), and correct stream. So it seems that there are references to that string and to that stream of `struct1`. But what holds the references? How does the runtime understands that the string and the stream must not be collected?