当前位置:网站首页>Garbage collection of C closure
Garbage collection of C closure
2022-06-25 00:23:00 【Moyiii】
Today, when analyzing the problems of project memory release and garbage collection , Noticed a problem , If a variable is referenced by a closure , Will not be recycled ( This is nonsense in theory ), That's it , This problem becomes very difficult to check . It means , Some buttons are bound to events with closures , This part of memory is hard to recycle . Here's an example :
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 Not recycled ");
}
else
{
Console.WriteLine("B It's recycled ");
}
}
}
class Program
{
public static void Main(string[] args)
{
TestClass c = new TestClass();
c.GenerateClosure();
GC.Collect();
c.OverWatchGC();
c.ReleaseClosure();
GC.Collect();
c.OverWatchGC();
}
}give the result as follows :
B Not recycled
B It's recycled
If the function where the closure is located is a static function , Recycling will be more troublesome , I'm testing , Even if you put action Set as null, Garbage collection has not been successful B References to .
So be careful, be careful, be careful , Sometimes a dot accumulates , It is a very difficult problem to check
Search for
Copy
边栏推荐
- Binder mechanism and Aidl communication example
- 断言(assert)的用法
- Decoupling pages and components using lifecycle
- Color gradient gradient color collection
- 无人驾驶: 对多传感器融合的一些思考
- U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency
- Signal integrity (SI) power integrity (PI) learning notes (XXV) differential pair and differential impedance (V)
- Modstart: embrace new technologies and take the lead in supporting laravel 9.0
- Eye gaze estimation using webcam
- Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source
猜你喜欢

UE4 WebBrowser图表不能显示问题

Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download

部门新来的00后真是卷王,工作没两年,跳槽到我们公司起薪18K都快接近我了

Apk slimming compression experience
Hyperledger Fabric 2. X dynamic update smart contract

VNC viewer remote connection raspberry pie without display

Applet opening traffic master

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

The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply

浅析大型IM即时通讯系统开发难度
随机推荐
Svg line animation background JS effect
Current situation and development prospect forecast report of global and Chinese tetrahydrofurfuryl alcohol acetate industry from 2022 to 2028
部门新来的00后真是卷王,工作没两年,跳槽到我们公司起薪18K都快接近我了
Virtual machine - network configuration
im即时通讯开发应用保活之进程防杀
Use of JMeter easynmon
Investment analysis and prospect forecast report of global and Chinese triglycine sulfate industry from 2022 to 2028
5G dtu无线通信模块的电力应用
Analysis report on operation trend and investment strategy of global and Chinese tetrahydrofurfuryl propionate industry from 2022 to 2028
Collective example
[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
Opengauss kernel: simple query execution
MySQL日志管理
I suddenly find that the request dependent package in NPM has been discarded. What should I do?
Design scheme of authority management of fusion model
[proteus simulation] example of using timer 0 as a 16 bit counter
Approaching harvest moon:moonbeam DFI Carnival
A small program written this week
Related operations of ansible and Playbook
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform