我需要在我的对象中实现处置或终结吗?
长期以来,我让垃圾收集器发挥其魔力,消除了我自己的所有责任。
遗憾的是,它从未成为一个问题……所以我从未再考虑过这个问题。
现在,当我思考它时,我真的不明白“dispose”函数的真正作用是什么以及应该如何以及何时实现它。
完成同样的问题......
最后一个问题...... 我有一个类 pictureManipulation :当我需要保存/调整大小/更改格式时...我启动该类的一个新实例,使用它的对象并且...让垃圾收集杀死该实例
class student
{
public void displayStudentPic()
{
PictureManipulation pm = new PictureManipulation();
this.studentPic = pm.loadStudentImage(id);
}
}
Class Test
{
student a = new Student();
a.displayStudentPic();
// Now the function execution is ended... does the pm object is dead? Will the GC will kill it?
}
For too long I let the garbage collector do its magic, removing all responsibilities from my self.
Sadly it never turned into an issue... So I never gave a second thought to the subject.
Now when I think about it I don't really understand what the "dispose" function really does and how and when it should be implemented.
The same question for finalize...
And a last question...
I have a class pictureManipulation : when I need to save/resize/change format ... I start a new instance of that class use its objects and... well let the garbage collection kill the instance
class student
{
public void displayStudentPic()
{
PictureManipulation pm = new PictureManipulation();
this.studentPic = pm.loadStudentImage(id);
}
}
Class Test
{
student a = new Student();
a.displayStudentPic();
// Now the function execution is ended... does the pm object is dead? Will the GC will kill it?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果对象持有的资源不仅仅是对象本身持有的内存,那么您需要注意对象的处置。
例如,如果你的对象抽象了一个文件,那么当文件被释放时你必须控制,否则,你会把事情搞砸得很糟糕:你的应用程序使用完它,它仍然会被锁定,直到 GC 处理你的对象。
要了解如何正确执行此操作,请阅读有关 dispose 和 Finalize 以及 using(){} 子句的手册。
You need to take care about object disposal if it holds resources other than just memory held by the object itself.
For instance, if your object abstracts a file, you must be in control when the file is released, or, you will mess things up very bad: your app finished using it and it will still be locked, until GC disposes your object.
To know how to do it properly, read manuals about dispose and finalize as well as the using(){} clause.
仅当您的类型拥有一些非托管资源(例如数据库连接、文件句柄等)或者您的类型拥有的某些对象实现了 IDisposable 接口时,您才需要实现 Dispose 方法。在实现标准 Dispose 模式时,您应该考虑以下几点:
You only need to implement the Dispose method if your type holds some unmanaged resources like DB connections, file handles, etc. or if some of the objects that are held by your type implement the IDisposable interface. Here is a few points you should consider when implementing the standard Dispose pattern:
关于您的
班级学生
假设 Picture 类是 IDisposable:是。因为 Student 对象“拥有”
studentPic
,这使得它负责清理它。一个最小的实现:现在您使用一个 Student 对象,例如:
如果您不能/不使用
using(){}
块,只需调用a.Dispose();< /code> 当你完成后。
但请注意,这里(远)更好的设计是避免将图片对象保留在 Student 对象中。这就引发了整个责任链。
不。因为当收集 Student 对象时,保证在同一运行中收集其 StudentPic 对象。终结器(析构函数)毫无意义,但仍然昂贵。
Regarding your
class Student
Assuming the Picture class is IDisposable: Yes. Because a Student object 'owns' the
studentPic
and that makes it responsible for cleaning it up. A minimal implementation:And you now use a Student object like:
If you can't/don't use a
using(){}
block, simply calla.Dispose();
when you're done with it.But please note that the (far) better design here would be to avoid keeping a picture object inside your Student object. That sets off a whole chain of responsibilities.
No. Because when a Student object is being collected, its studentPic object is guaranteed to be collected in the same run. A Finalizer (destructor) would be pointless but still expensive.