C#释放资源
答案:6 悬赏:30 手机版
解决时间 2021-04-05 06:34
- 提问者网友:听门外雪花风
- 2021-04-04 18:27
C#释放资源
最佳答案
- 五星知识达人网友:愁杀梦里人
- 2021-04-04 19:13
Dispose是用来释放非托管资源的,并不会释放托管资源,new是从托管堆中分配得资源,只能通过GC回收,无法手动释放。你可以使用System.GC.Collect()来强制进行GC,用System.GC.WaitForFullGCComplete()等待GC完成。
如果你确实需要,使用System.Runtime.InteropServices.Marshal来手动申请和释放非托管内存,但是,其中无法保存托管对象,只能保存整型、浮点型和字符型及其数组,或者具有StructLayout或MarshalAs特性的结构或类。 (会带来一定的运行期代价)
如果你确实需要,使用System.Runtime.InteropServices.Marshal来手动申请和释放非托管内存,但是,其中无法保存托管对象,只能保存整型、浮点型和字符型及其数组,或者具有StructLayout或MarshalAs特性的结构或类。 (会带来一定的运行期代价)
全部回答
- 1楼网友:孤独入客枕
- 2021-04-04 23:25
强制性的GC.Collect()
但微软不推荐这么用
但微软不推荐这么用
- 2楼网友:归鹤鸣
- 2021-04-04 21:57
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.
- 3楼网友:長槍戰八方
- 2021-04-04 21:31
不需要手动释放,你可以使用完之后,就test = null,这是一个好习惯,下次.net自动GC时就会把这些资源给回收掉。
所以说,你是不用手动去释放的。
所以说,你是不用手动去释放的。
- 4楼网友:神鬼未生
- 2021-04-04 20:41
C#的设计者不允许开发者自行回收垃圾,没有任何语法支持直接垃圾回收,只有Runtime才能决定何时释放资源。可以使用unsafe,using代码块以及Dispose方法来实现资源管理(自己查),也可以使用System.GC.Collect来强制垃圾回收,但是MS不推荐这样做,因为当方法结束时,你还是不知道对象是否已经被摧毁!!!!!!!!!!!!!!!!!
另外,如果要一个值类型的值为null,比如一个Int值,声明时必须这样写(加问号):int? myint;
另外,如果要一个值类型的值为null,比如一个Int值,声明时必须这样写(加问号):int? myint;
- 5楼网友:不想翻身的咸鱼
- 2021-04-04 20:33
资源释放,C#一般是使用自带的GC,如果想自己实现包含dispose方法的类型,可以手动实现IDisposable接口,不过这个接口的实现很有技巧性,需要自己写一个释放托管的和释放非托管的!
如果想直接在使用完数组就释放掉,一般可以使用unsafe代码块,或者更底层的换个语言来做这样大数据量计算的模块!
如果想直接在使用完数组就释放掉,一般可以使用unsafe代码块,或者更底层的换个语言来做这样大数据量计算的模块!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯