当前位置:网站首页>C# 闭包的垃圾回收
C# 闭包的垃圾回收
2022-06-24 19:50:00 【Moyiii】
今天在分析项目内存释放和垃圾回收问题的时候,注意到了一个问题,如果一个变量被闭包引用,则不会被垃圾回收掉(这理论上是废话),这正是这样,这个问题才变得很难查。这意味着,一些按钮绑定了带闭包的事件,这部分内存就很难回收了。可以看一个例子:
public class B
{
public int Name;
}
class TestClass
{
private Action action;
private static WeakReference<B> weak;
public void GenerateClosure()
{
B b = new B();
weak = new WeakReference<B>(b);
action = () =>
{
Console.WriteLine(b.Name);
};
}
public void ReleaseClosure()
{
action = null;
}
public void OverWatchGC()
{
if (weak.TryGetTarget(out B b))
{
Console.WriteLine("B未被回收");
}
else
{
Console.WriteLine("B被回收了");
}
}
}
class Program
{
public static void Main(string[] args)
{
TestClass c = new TestClass();
c.GenerateClosure();
GC.Collect();
c.OverWatchGC();
c.ReleaseClosure();
GC.Collect();
c.OverWatchGC();
}
}结果如下:
B未被回收
B被回收了
如果闭包所在的函数是静态函数,则回收还会更麻烦,我在测试中,即便把action置为null,垃圾回收也没有成功回收掉对B的引用。
所以要谨慎谨慎再谨慎,有时候一个小点累积下去,就是一个非常难排查的问题
搜索
复制
边栏推荐
- Svg+js keyboard control path
- svg+js键盘控制路径
- [proteus simulation] example of using timer 0 as a 16 bit counter
- Discrete mathematics and its application detailed explanation of exercises in the final exam of spring and summer semester of 2018-2019 academic year
- 软件测试与游戏测试文章合集录
- Hyperledger Fabric 2. X dynamic update smart contract
- JPA learning 2 - core annotation, annotation addition, deletion, modification and query, list query result return type, one to many, many to one, many to many
- Hibernate learning 3 - custom SQL
- Use of JMeter
- Dry and wet contacts
猜你喜欢

离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解

Power application of 5g DTU wireless communication module

UE4 WebBrowser chart cannot display problems

信号完整性(SI)电源完整性(PI)学习笔记(二十五)差分对与差分阻抗(五)

How does VR panorama make money? Based on the objective analysis of the market from two aspects

Hibernate learning 2 - lazy loading (delayed loading), dynamic SQL parameters, caching

What exactly is Nacos

5G dtu无线通信模块的电力应用

The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply
Hyperledger Fabric 2. X dynamic update smart contract
随机推荐
Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
Current situation and development prospect forecast report of global and Chinese tetrahydrofurfuryl alcohol acetate industry from 2022 to 2028
时间统一系统
Im instant messaging development application keeping alive process anti kill
Dry and wet contacts
融合模型权限管理设计方案
微搭低代码中实现增删改查
MySQL日志管理
Overview of medium and low speed aerospace electronic bus
In the past 5 years, from "Diandian" to the current test development, my success is worth learning from.
第三代电力电子半导体:SiC MOSFET学习笔记(五)驱动电源调研
Use coordinatorlayout+appbarlayout+collapsingtoolbarlayout to create a collapsed status bar
How can I persuade leaders to use DDD to construct the liver project?
Eye gaze estimation using webcam
im即时通讯开发应用保活之进程防杀
Hibernate learning 2 - lazy loading (delayed loading), dynamic SQL parameters, caching
Use and click of multitypeadapter in recycleview
Analysis report on development trend and investment forecast of global and Chinese D-leucine industry from 2022 to 2028
JMeter socket connection sends data
Apk slimming compression experience