永发信息网

C#释放资源

答案:6  悬赏:30  手机版
解决时间 2021-04-05 06:34
C#释放资源
最佳答案
Dispose是用来释放非托管资源的,并不会释放托管资源,new是从托管堆中分配得资源,只能通过GC回收,无法手动释放。你可以使用System.GC.Collect()来强制进行GC,用System.GC.WaitForFullGCComplete()等待GC完成。
如果你确实需要,使用System.Runtime.InteropServices.Marshal来手动申请和释放非托管内存,但是,其中无法保存托管对象,只能保存整型、浮点型和字符型及其数组,或者具有StructLayout或MarshalAs特性的结构或类。 (会带来一定的运行期代价)
全部回答
强制性的GC.Collect()
但微软不推荐这么用
public class BaseResource: IDisposable
{
// Pointer to an external unmanaged resource.[Page]
// 非托管资源
private IntPtr handle;
// Other managed resource this class uses.
// 托管资源
private Component Components;
// Track whether Dispose has been called.
// 是否已经释放资源的标志
private bool disposed = false;
// Constructor for the BaseResource object.
public BaseResource()
{
// Insert appropriate constructor code here.
}
// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
// 提供给外部用户显示调用的方法,实际操作是在类的带参数的虚函数Dispose(bool disposing)中实现
public void Dispose()
{
// 表示用户显示调用
Dispose(true);
// Take yourself off the Finalization queue
// to prevent finalization code for this object
// from executing a second time.
// 由于用户是显示调用,所以资源释放不再由GC来完成
GC.SuppressFinalize(this);
}
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user\'s code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
// 如果已经释放,不做再次的操作,出现在用户多次调用的情况下
if(!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
// Dispose managed resources.
// 用户是显示调用的话,我们就要手工的操作托管资源
Components.Dispose();
}
// Release unmanaged resources. If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr.Zero;
// Note that this is not thread safe.
// Another thread could start disposing the object
// after the managed resources are disposed,
// but before the disposed flag is set to true.
// If thread safety is necessary, it must be[Page]
// implemented by the client.
}
disposed = true;
}
// Use C# destructor syntax for finalization code.
// This destructor will run only if the Dispose method
// does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide destructors in types derived from this class.
// 析构函数
~BaseResource()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
// 表示本次调用是隐式调用,由Finalize方法调用,即托管资源释放由GC来完成
Dispose(false);
}
// Allow your Dispose method to be called multiple times,
// but throw an exception if the object has been disposed.
// Whenever you do something with this class,
// check to see if it has been disposed.
public void DoSomething()
{
if(this.disposed)
{
throw new ObjectDisposedException();
}
}
}
// Design pattern for a derived class.
// Note that this derived class inherently implements the
// IDisposable interface because it is implemented in the base class.
public class MyResourceWrapper: BaseResource
{
// A managed resource that you add in this derived class.
private ManagedResource addedManaged;
// A native unmanaged resource that you add in this derived class.
private NativeResource addedNative;
private bool disposed = false;
// Constructor for this object.
public MyResourceWrapper()
{
// Insert appropriate constructor code here.
}
// 重写Dispose方法,释放派生类自己的资源,并且调用基类的Dispose方法
protected override void Dispose(bool disposing)
{
if(!this.disposed)
{
try
{
if(disposing)
{
// Release the managed resources you added in
// this derived class here.
addedManaged.Dispose();
}
// Release the native unmanaged resources you added
// in this derived class here.
CloseHandle(addedNative);
this.disposed = true;[Page]
}
finally
{
// Call Dispose on your base class.
base.Dispose(disposing);
}
}
}
}
// 在这里,派生类没有实现~MyResourceWrapper和public Dispose方法,应为他们已经继承了基类的这些特性,这也是我说本示例代码精要之处,他使用到了多态性原理,下面我会简单分析
// This derived class does not have a Finalize method
// or a Dispose method without parameters because it inherits
// them from the base class.
不需要手动释放,你可以使用完之后,就test = null,这是一个好习惯,下次.net自动GC时就会把这些资源给回收掉。
所以说,你是不用手动去释放的。
C#的设计者不允许开发者自行回收垃圾,没有任何语法支持直接垃圾回收,只有Runtime才能决定何时释放资源。可以使用unsafe,using代码块以及Dispose方法来实现资源管理(自己查),也可以使用System.GC.Collect来强制垃圾回收,但是MS不推荐这样做,因为当方法结束时,你还是不知道对象是否已经被摧毁!!!!!!!!!!!!!!!!!
另外,如果要一个值类型的值为null,比如一个Int值,声明时必须这样写(加问号):int? myint;
资源释放,C#一般是使用自带的GC,如果想自己实现包含dispose方法的类型,可以手动实现IDisposable接口,不过这个接口的实现很有技巧性,需要自己写一个释放托管的和释放非托管的!
如果想直接在使用完数组就释放掉,一般可以使用unsafe代码块,或者更底层的换个语言来做这样大数据量计算的模块!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
格力32W/FNB14-A3是什么制冷剂
健身私教好找工作吗
乔一碗凉皮好吃吗?
TheFrenchRevolutionwassuccessfulin
不珍惜我的人经典语句,夸奖男人珍惜女人的句
在西安哪个车站可以坐到直接到宝鸡汽车西站的
明朝的哪个皇帝跫是放羊长大的?
滴滴快车怎么申请加入,怎样加入武汉志愿者协
有人问我为什么这黑 我该咋回答
哈麻狗喜欢吃什么,狗粮会吃吗
单选题1004年,辽军大举进攻北宋时,北宋主张
幼儿园毕业感言寄语,幼儿园毕业感言感恩父母
有一天,你到姑姑家做客,发现姑姑的一封信上
诛仙所有服务器开服时间表
求上级办事,微信请示怎样
推荐资讯
网上招标与普通招标有哪些不同?
单选题____ the factors already&
如果∠α=40°,那么∠α的补角等于________
岁月无声 怎样理解这句话?
德国洁水是德国原装的吗
word怎么设置页码从正文开始
单选题氰气[(CN)2]和氰化物都是剧毒性物质,
形容文章写得好的诗词句
为什么我的c4d一载入纹理物体就变黑 所有资源
跳蚤市场班级口号,幼儿园跳蚤市场喊麦口号
雷克萨斯es250舒适版2014款是真皮座椅吗
请问成为科学家需要学历吗。
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?