C#类的序列化或反序列化方法导致大量GC

时间:2019-01-01 22:24:53

标签: c# unity3d serialization garbage-collection

我正在制作2D箱加载系统, 所以我做香客类,并序列化或反序列化此类到文件 但是,我有一个大问题,当我使用方法,序列化或反序列化时,此方法会导致巨大的GC分配,因此,我的游戏速度会变慢。 所以,我想序列化导致GC或我的代码有问题,所以,序列化导致GC

我检查了反序列化原因GC

public int[,] chank_Collision_Getter(int x,int y)
{
    BinaryFormatter bf = new BinaryFormatter();
    TextAsset target_File = (TextAsset)Resources.Load("Base_Chank");
    byte[] file_Bytes = target_File.bytes;
    Stream file = new MemoryStream(file_Bytes);

    if (file != null && file.Length > 0)
    {
        read_Only_Chank = (ChankStruct)bf.Deserialize(file);//GC Alloc

    }

    file.Close();
    return read_Only_Chank.collision_Getter();
}

如何减少反序列化引起的GC

0 个答案:

没有答案