在什么条件下 Marshal.SizeOf 在堆上分配?

Under which conditions Marshal.SizeOf allocates on the heap?

所以我有来自 dotTrace 的非常奇怪的跟踪数据:

结构如下:

public struct TargetStruct : SomeInterface
{
    private RigidTransform rt;
    public RoundBounds rb;
    public int Start;
    public Bool IsEnabled;
    public TypeEnum TheType;
}
//
// Nested types
//
public struct RoundBounds : SomeInterface
{
    public float3 Center;
    public float Radius;
    public float RadiusSq;
}
public struct Bool : IEquatable<Bool>, SomeInterface
{
    [MarshalAs(UnmanagedType.I1)]
    private bool value;
}
public enum TypeEnum : byte
{
    None,
    Type1,
    Type1
}
public struct RigidTransform
{
    public quaternion rot;
    public float3 pos;
    public static readonly RigidTransform identity = new RigidTransform(new quaternion(0f, 0f, 0f, 1f), new float3(0f, 0f, 0f));
}

这在 Unity 的单声道 2019.4.12-mbe 下运行,如果相关,Unity 会生成针对 4.7.1 的项目。 Struct 正在通过具有约束 where T: struct 的通用方法传递给 SizeOf,尽管这应该是无关紧要的。

这是 mono 的 mscorlib 的 IL Marshal.SizeOf<T> 呼叫转移到外部 SizeOf(Type t);

什么可能导致堆分配?我唯一的假设是拳击,但我在这里看不到任何拳击。 GetType()should not allocate

你提到的关于 GetType() 的分配行为的 link 说它不会每次都分配一个新实例,它确实 而不是 说它根本不分配。

第一次对特定类型的对象调用 GetType() 时,将分配一个新的 RuntimeType 实例来表示该类型,但这是一次性分配,随后GetType() 对该类型对象的调用将 return 现有实例。

使用 dotMemory 并在启用“从开始收集内存分配和流量数据”选项的情况下分析您的应用程序。然后看"Allocations" view。它会以 byte/object.

的精度向您显示所有分配

你能告诉我 dotMemory/dotTrace 中的数据是否与你的情况不同吗?