C#中的弱引用使用

**弱引用(Weak Reference)**是一种特殊的引用类型,它允许你引用一个对象,但不会阻止该对象被垃圾回收器(GC)回收。弱引用通常用于需要缓存或跟踪对象,但又不希望因保留引用而导致内存泄漏的场景。弱引用对小对象没有意义,因为弱引用有自己的开销,这个开销可能比小对象更大。

核心概念​​

​​与强引用的区别​​ :
​​强引用​​ :普通对象引用(如 var obj = new MyClass())会阻止 GC 回收对象。
​​弱引用 ​​:不会阻止 GC 回收对象。当对象只有弱引用时,GC 可以随时回收它。
​​用途​​ :

缓存大型数据(如图片、文件等),当内存不足时自动释放。

避免因长期持有对象引用而导致内存泄漏。

监听事件或观察对象生命周期,但不干预其回收。

使用:

1.基础使用:

代码:

cs 复制代码
public class WeakReferenceTest : MonoBehaviour
{
    WeakReference weakRef;
    // Start is called before the first frame update
    void Start()
    {
        // 创建一个对象
        var myObject = new TestWeakReferenceObj();

        // 创建弱引用
        weakRef = new WeakReference(myObject);

        // 解除强引用,只保留弱引用
        myObject = null;

        // 通过弱引用访问对象
        if (weakRef.IsAlive)
        {
            var obj = weakRef.Target as TestWeakReferenceObj;
            obj.DoSomething();
        }
        else
        {
            Debug.Log("对象已被回收");
        }
    }

    // Update is called once per frame
    void Update()
    {
        // 通过弱引用访问对象
        if (weakRef.IsAlive)
        {
            var obj = weakRef.Target as TestWeakReferenceObj;
            obj.DoSomething();
        }
        else
        {
            Debug.Log("对象已被回收");
        }
    }
}

class TestWeakReferenceObj { 
    public void DoSomething()
    {
        Debug.Log("对象没有被回收");
    }
}

结果:

2.泛型使用:

代码:

cs 复制代码
public class WeakReferenceTest : MonoBehaviour
{
    WeakReference<TestWeakReferenceObj> weakRefGeneric;
    // Start is called before the first frame update
    void Start()
    {
        weakRefGeneric = new WeakReference<TestWeakReferenceObj>(new TestWeakReferenceObj());

        if (weakRefGeneric.TryGetTarget(out TestWeakReferenceObj obj))
        {
            obj.DoSomething();
        }
        else
        {
            Debug.Log("对象已被回收");
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (weakRefGeneric.TryGetTarget(out TestWeakReferenceObj obj))
        {
            obj.DoSomething();
        }
        else
        {
            Debug.Log("对象已被回收");
        }
    }
}

class TestWeakReferenceObj { 
    public void DoSomething()
    {
        Debug.Log("对象没有被回收");
    }
}

结果:

适用场景:

​​缓存​ ​:缓存大型对象,但允许 GC 在需要时回收它们。
事件监听 ​​:避免事件订阅导致的内存泄漏。

​​对象生命周期跟踪​​:观察对象是否存活,但不阻止其回收。

注 :
性能 ​​:频繁检查弱引用是否存在可能影响性能。
​​不确定性 ​​:对象可能在任何时候被回收,需始终检查 IsAlive 或 TryGetTarget。
​​短期弱引用 (Short Weak Reference)​​:默认行为,GC 回收后弱引用自动失效。
​​长期弱引用(Long Weak Reference)​​:通过 WeakReference 构造函数指定 trackResurrection 为 true,可跟踪对象在终结后的状态(但极少使用)。

总结:

弱引用是 C# 中管理内存的一种高级机制,适用于需要灵活控制对象生命周期的场景。正确使用它可以优化内存使用,但需谨慎处理对象可能随时被回收的情况。

参考书籍或链接:

《C#高级编程》第2版

相关推荐
huaqianzkh20 小时前
WMI 查询入门到实战:从概念、WQL 语法到 C# 采集系统与硬件信息完整示例
开发语言·c#
2395730491 天前
Avalonia 12 代码编辑器性能横评:TKCode.Arc CodeEditor vs AvaloniaEdit 12.0(含深度调优实战)
c#·.net·avalonia·codeeditor·tkcode.arc
张人玉3 天前
C#如何使用FreeSql连接数据库
数据库·c#·freesql
吴可可1234 天前
C#打断多段线实现方法
c#
Kari114 天前
医药和制造行业的外文文档批量翻译,排版怎么保住?
开发语言·c#·制造
Warson_L4 天前
.NET 8 速成笔记 1:用 VS Code 做 C# / .NET 开发
c#·.net
Warson_L4 天前
.NET 8 速成笔记 9:C# 项目常见场景(基础设施篇)
c#·.net
Warson_L4 天前
.NET 8 速成笔记 8:C# 项目常见业务场景与写法(数据与业务篇)
c#·.net
Warson_L4 天前
.NET 8 速成笔记 3:作业清单
c#·.net
Warson_L4 天前
.NET 8 速成笔记 2:C# / .NET 核心概念(结合本项目代码)
c#·.net