关于C#List<Struct>的Remove操作GC问题

26 篇文章 3 订阅

本文主要探讨关于C#中对值类型的List.Remove操作的GC问题。

一、先说结论:

1、【🔴】纯结构体,啥都不做,gc很多。
2、【🔴】结构体实现IComparable接口的CompareTo(Struct2 other) ,无法优化掉gc。
3、【🟢】结构体实现IEquatable接口的Equals(Struct3 other),gc为0。
4、【🟣】结构体实现IEquatable接口和IComparable接口,gc为0(由234可以得出,只要实现IEquatable接口即可优化掉gc)
5、【🟣】结构体重写Equals(object other)方法,少量gc(存在装箱)
6、【🔴】结构体只重写==和!=操作符的结果跟1一样。

二、执行以下方法查看gc情况:

List<Struct1> list1 = new List<Struct1>();
List<Struct2> list2 = new List<Struct2>();
List<Struct3> list3 = new List<Struct3>();
List<Struct4> list4 = new List<Struct4>();
List<Struct5> list5 = new List<Struct5>();

// X代表12345
void UpdateX()
{
    for (int i = 0; i < 100; i++)
    {
        listX.Add(new StructX());
    }
    
    for (int i = 0; i < listX.Count; i++)
    {
        listX.Remove(listX[0]);
    }
}

三、结果:

 四、具体实现

// 啥都不做 100次remove gc 15.6kb
struct Struct1
{
    public int num;
    public bool isUsed;
}

// 实现IComparable接口  100次remove gc 15.6kb
struct Struct2 : IComparable<Struct2>
{
    public int num;
    public bool isUsed;

    int IComparable<Struct2>.CompareTo(Struct2 other)
    {
        return this.num - other.num;
    }
}

// 实现IEquatable接口  100次remove gc 0kb
struct Struct3 : IEquatable<Struct3>
{
    public int num;
    public bool isUsed;

    bool IEquatable<Struct3>.Equals(Struct3 other)
    {
        return this.num == other.num;
    }
}

// 实现IEquatable接口和IComparable接口  100次remove gc 0kb (从这里可以得出,只要实现IEquatable接口即可优化掉gc)
struct Struct4 : IComparable<Struct4>, IEquatable<Struct4>
{
    public int num;
    public bool isUsed;

    int IComparable<Struct4>.CompareTo(Struct4 other)
    {
        return this.num - other.num;
    }

    bool IEquatable<Struct4>.Equals(Struct4 other)
    {
        return this.num == other.num;
    }
}

// 重写Equals方法 100次remove gc 3.1kb(装箱)
struct Struct5
{
    public int num;
    public bool isUsed;

    public override bool Equals(object obj)
    {
        return obj is Struct5 t &&
               num == t.num;
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值