当前位置:网站首页>C memory leak 1: class with dispose method
C memory leak 1: class with dispose method
2022-04-22 21:29:00 【Kingston】
Reprinted address :https://zhuanlan.zhihu.com/p/365075416
GC and Dispose
C#, Although there are GC System , Can automatically reclaim free memory ( Managed memory ), But there are many classes Dispose Method , Used to manually and explicitly release the memory occupied by the instance of this class ( Managed and unmanaged memory )
Unmanaged resources must be explicitly released
Managed memory can wait for the system GC Release , You can also explicitly release , The main significance of explicit release is to control the timing of release , Free up more free memory immediately
Inherit IDisposable Self defined subclass , If a destructor is defined, you can free memory in the destructor , But the destructor is composed of GC Thread calls , Time and sequence are uncontrollable , You can also customize it Dispose Method , Release manually when needed , But I need to tell GC The system has released this resource , And at the same time GC Coordinate the release sequence , Sample code
public class DisposablClass : IDisposable
{
// Whether the recycling is completed
bool _disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this); // Mark gc No more calling destructors
}
~DisposableClass()
{
Dispose(false);
}
private void Dispose(bool disposing)
{
if(_disposed) return; // If it has been recycled , Just interrupt execution
if(disposing)
{
//TODO: Release the managed resources managed in this object
}
//TODO: Release unmanaged resources
_disposed = true;
}
}
Conditions that may cause memory leaks
1 In explicit call Dispose Before method , Abnormal exit of program
2 There is no destructor defined , But it doesn't show the call Dispose Method
terms of settlement
Use using To create a new class object instance , Sample code
using (var instance = new MyClass())
{
// ...
}
The above code is equivalent to , Whether the program is abnormal or not , Will eventually be released instance Of memory
var instance = new MyClass();;
try
{
// ...
}
finally
{
if (instance != null)
instance.Dispose();
}
Reference documents :https://www.cnblogs.com/murongxiaopifu/p/12894385.html
https://www.cnblogs.com/xyzf/p/9021513.html
版权声明
本文为[Kingston]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222126342757.html
边栏推荐
- Select distinct statement de duplication
- How to write word reading and color games based on JS
- Dernière version du filigrane + code source de l'applet CPS à emporter
- golang 入门--定义map的6种方式
- 如何10分钟之内配置好数字门户,集中统一全方位呈现
- 简单介绍常用数据库引擎
- 實驗3
- [bat] view the file MD5
- Big talk test data (II): acquisition of concept test data
- Tensorflow Basics
猜你喜欢

Thread communication mechanism: shared memory VS message passing

Reverse entry (III) save CT table and generate exe modifier after CE automatic assembly

Basic design of character animation based on paogd

实验3

MySQL尚硅谷课程查缺补漏适合基础(一)

Huawei interview question: "how can an 800kg cow cross a 700kg bridge?" You can live your life as you think

Openvx's immediate mode and graph mode and examples

Embedded design and development project - digital tube static display program design

Hdlbits (10) learning notes - finite state machine (fsm1 - lemmings4)

Future source code | Professor Wu Enda's lecture: Tips for using a data centric AI approach
随机推荐
R language data reading, cleaning and univariate linear regression
glib 和 glibc
Experiment 3
改善C#程序的建议5:引用类型赋值为null与加速垃圾回收
Tensorflow Basics
Reflection: newinstance () gets the specific class object interface according to the package name and class name, and calls the method in the specified class
Tool class xmlutil (parse and return soap and XML messages to obtain the value of the target node)
After five years of graduation, how to increase the monthly salary from 5K to 50W +, what core skills do you need to master?
Spark-2.4.2编译安装
. net core using efcore in multiple projects
QT uses windeployqt Exe packaging program
新聞速遞 I MobTech通過中國信通院“安全專項評測”
Based on paogd_ HW1's pop-up ball - simple modeling, interpolation animation
[IPTV] Huawei Yuehe ec6108v9a brush machine
MySQL specifies field sorting and user-defined sorting location
Spark-2.4.2 compilation and installation
autodesk产品无法安装解决方案
Fiddler - tamper with the data returned by the server
Summary of problems encountered in ffmpeg audio decoding
简单介绍常用数据库引擎