Unity的分析器显示每次调用StringBuilder.AppendLine时都会分配30个字节的垃圾,因为它访问System.Environment.NewLine。
所以我的问题:
编辑:我刚刚在Unity的mscorlib.dll上使用了IL spy并找到了该属性的来源:
private static string nl;
...
public static string NewLine
{
get
{
if (Environment.nl != null)
{
return Environment.nl;
}
Environment.nl = Environment.GetNewLine();
return Environment.nl;
}
}
...
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern string GetNewLine();
即使它在第一次通话时产生了一些垃圾,也不可能每次都做得更多。看起来好像是分析器是错误的,但它让我想知道如何发生这种情况。