【C#】关于Array.Copy 和 GC

文章讨论了在.NET中,当源和目标数组类型不一致,如从byte到short或int时,使用Array.Copy可能导致垃圾回收(GC)。作者推测可能由于拆箱操作触发,建议在实际开发中确保类型匹配以避免性能问题。
摘要由CSDN通过智能技术生成

关于Array.Copy 和 GC

	//一个简单的 数组copy   什么情况下会触发GC呢
    [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
    public static void Copy(Array sourceArray,
                            long sourceIndex,
                            Array destinationArray,
                            long destinationIndex,
                            long length);

当源和目标的类型不一致,由小转大,比如由byte 到 short ,int 都会触发GC ,我不知道内部机制如何,可能是拆装箱导致的 ,不确定,不过在实际开发中确实出现了这种问题,所以使用的时候 类型要匹配

贴一段测试代码

   void Update()
    {
        var start = DateTime.Now;
        for (int i = 0; i < this.inputData.Length; i++)
        {
            this.inputData[i] = -125;
            var a = Mathf.Abs(this.inputData[i]);
            a = Mathf.Clamp(a, 0, 1023);
            this.inputData[i] = (short)(a * 255 / 1023);
        }
        Debug.Log($"<color=#ff00ff>CPU cost : {(DateTime.Now - start).TotalMilliseconds}</color>");

        start = DateTime.Now;
        **byte[] outData = new byte[size];**
        this.ComputeData(ref outData); //GPU

        int[] outData2 = new int[this.size];
        Array.Copy(outData, 0, outData2, 0, outData.Length);
        
        Debug.Log($"<color=#ffff00>GPU cost : {(DateTime.Now - start).TotalMilliseconds}</color>");
    }

    private void ComputeData(ref int[] outPutBytes)
    {
        //if (this.inputbuffer == null)
        {
            inputbuffer = new ComputeBuffer(this.inputData.Length, 4); //定义缓冲区(参数:数组长度、每个数组元素占用字节数)
        }

        //if (this.outputbuffer == null)
        {
            outputbuffer = new ComputeBuffer(this.outputData.Length, 4);
        }

        inputbuffer.SetData(this.inputData); //待计算数据加载入缓冲区
        this.shader.SetBuffer(this.k, "inputData", inputbuffer); //定义输入口
        this.shader.SetBuffer(this.k, "outputData", outputbuffer); //定义输出口

        this.shader.Dispatch(this.k, this.inputData.Length / 1024, 1, 1); //开始执行(参数:主函数下标、线程组的XYZ个数)
        outputbuffer.GetData(this.outputData); //缓冲区获得计算好的数据

        for (int i = 0; i < this.outputData.Length; i++)
        {
            outPutBytes[i] = (byte)this.outputData[i];
        }

        inputbuffer.Dispose(); //清除缓存
        outputbuffer.Dispose();
    }

运行情况
在这里插入图片描述
GC

在这里插入图片描述
恐怖如斯啊~~~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值