C#释放资源
假设有这么一条语句:int[]test=newint[100];在使用完后如何释放它所占用的资源?谢谢。我知道C#会自动回收资源。但有没有办法手动释放。就像很多类提供的d...
假设有这么一条语句: int[] test=new int[100];
在使用完后如何释放它所占用的资源?谢谢。
我知道C#会自动回收资源。但有没有办法手动释放。就像很多类提供的dispose()方法那样。 展开
在使用完后如何释放它所占用的资源?谢谢。
我知道C#会自动回收资源。但有没有办法手动释放。就像很多类提供的dispose()方法那样。 展开
9个回答
展开全部
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.
{
// 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.
展开全部
如果你是新手的话,尽量不要尝试手动释放,事实上,C#的自动释放是很优秀的。当然了,有很多情况下,我们对资源的把握需要我们卸载之前的一些资源用来用来继续使用,就像很多类都提供了卸载的一些方法,我常常在自己的类里写一个任意命名的方法,用作卸载,且将这个方法在该类的自动卸载中引用(事实上,很多情况要自己卸载,比如套接字)可以保证自动卸载时他被卸载。
综上所述,C#中只有少部分需要自行卸载,其他都可以由其自动卸载。当然,如果你是大师级,以上全是我的梦游之语。
综上所述,C#中只有少部分需要自行卸载,其他都可以由其自动卸载。当然,如果你是大师级,以上全是我的梦游之语。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Dispose是用来释放非托管资源的,并不会释放托管资源,new是从托管堆中分配得资源,只能通过GC回收,无法手动释放。你可以使用System.GC.Collect()来强制进行GC,用System.GC.WaitForFullGCComplete()等待GC完成。
如果你确实需要,使用System.Runtime.InteropServices.Marshal来手动申请和释放非托管内存,但是,其中无法保存托管对象,只能保存整型、浮点型和字符型及其数组,或者具有StructLayout或MarshalAs特性的结构或类。 (会带来一定的运行期代价)
如果你确实需要,使用System.Runtime.InteropServices.Marshal来手动申请和释放非托管内存,但是,其中无法保存托管对象,只能保存整型、浮点型和字符型及其数组,或者具有StructLayout或MarshalAs特性的结构或类。 (会带来一定的运行期代价)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
资源释放,C#一般是使用自带的GC,如果想自己实现包含dispose方法的类型,可以手动实现IDisposable接口,不过这个接口的实现很有技巧性,需要自己写一个释放托管的和释放非托管的!
如果想直接在使用完数组就释放掉,一般可以使用unsafe代码块,或者更底层的换个语言来做这样大数据量计算的模块!
如果想直接在使用完数组就释放掉,一般可以使用unsafe代码块,或者更底层的换个语言来做这样大数据量计算的模块!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
C#的设计者不允许开发者自行回收垃圾,没有任何语法支持直接垃圾回收,只有Runtime才能决定何时释放资源。可以使用unsafe,using代码块以及Dispose方法来实现资源管理(自己查),也可以使用System.GC.Collect来强制垃圾回收,但是MS不推荐这样做,因为当方法结束时,你还是不知道对象是否已经被摧毁!!!!!!!!!!!!!!!!!
另外,如果要一个值类型的值为null,比如一个Int值,声明时必须这样写(加问号):int? myint;
另外,如果要一个值类型的值为null,比如一个Int值,声明时必须这样写(加问号):int? myint;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询