当前位置:网站首页>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的引用。
所以要谨慎谨慎再谨慎,有时候一个小点累积下去,就是一个非常难排查的问题
搜索
复制
边栏推荐
- How to delete the entire row with duplicate items in a column of WPS table
- Dry and wet contacts
- C WinForm maximizes occlusion of the taskbar and full screen display
- [interview question] the difference between instancof and getclass()
- Do280openshift access control -- encryption and configmap
- UE4 WebBrowser chart cannot display problems
- Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
- @mysql
- Unmanned driving: Some Thoughts on multi-sensor fusion
- 信号完整性(SI)电源完整性(PI)学习笔记(二十五)差分对与差分阻抗(五)
猜你喜欢
融合模型权限管理设计方案
Power application of 5g DTU wireless communication module
Color gradient gradient color collection
Creative SVG ring clock JS effect
Use coordinatorlayout+appbarlayout+collapsingtoolbarlayout to create a collapsed status bar
C# Winform 最大化遮挡任务栏和全屏显示问题
JDBC —— 数据库连接
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply
MySQL log management
随机推荐
Jar package merging using Apache ant
Alternative to log4j
Use and click of multitypeadapter in recycleview
Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
im即时通讯开发应用保活之进程防杀
离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解
JS dynamically generates variable names and assigns values
信号完整性(SI)电源完整性(PI)学习笔记(二十五)差分对与差分阻抗(五)
Hibernate learning 3 - custom SQL
Why are life science enterprises on the cloud in succession?
Report on operation pattern and future prospect of global and Chinese propyl isovalerate industry from 2022 to 2028
Binder mechanism and Aidl communication example
Go crawler framework -colly actual combat (I)
Im instant messaging development application keeping alive process anti kill
JMeter socket connection sends data
The drawableleft of the custom textview in kotlin is displayed in the center together with the text
Investment analysis and prospect forecast report of global and Chinese triglycine sulfate industry from 2022 to 2028
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
C WinForm maximizes occlusion of the taskbar and full screen display
Hyperledger Fabric 2. X dynamic update smart contract