C# 的“新”现有对象的关键字
我想知道一旦对象的引用被重新分配,它会发生什么(在 C# 中)。示例:
Car c = new Car("Red Car");
c = new Car("Blue Car");
由于引用被重用,垃圾收集器在丢失引用后是否处置/处理“红色汽车”?或者是否需要实施单独的方法来处理“红色汽车”?
我主要想知道,因为我要回收一个相对较大的对象,并且需要知道重新创建它时是否应该执行任何操作。
I was wondering as to what happens to an object (in C#), once its reference becomes reassigned. Example:
Car c = new Car("Red Car");
c = new Car("Blue Car");
Since the reference was reused, does the garbage collector dispose / handle the 'Red Car' after it's lost its reference? Or does a separate method need to be implemented to dispose of the 'red car'?
I'm primarily wondering because there's a relatively large object that I'm going to recycle, and need to know if there is anything that should be done when it gets recreated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在您的示例中,当将
c
分配给Blue Car
时,c
的Red Car
实例将有资格进行垃圾回收>。你不需要做任何事情。查看这篇关于 .NET 垃圾收集器的(旧的,但仍然相关)MSDN 文章。 http:// msdn.microsoft.com/en-us/magazine/bb985010.aspx
第一段说明了一切:
In your example, the
Red Car
instance ofc
will become eligible for garbage collection whenc
is assigned toBlue Car
. You don't need to do anything.Check out this (old, but still relevant) MSDN article about the .NET garbage collector. http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
The first paragraph says it all:
您可能以错误的方式看待这个问题:
然后您的下一步:
GC 将出现并“收集”任何具有 在未来的某个时刻没有对活动对象的引用链。对于大多数任务,只要您使用托管数据,您就不必担心大对象与小对象。
对于大多数任务,您在处理
IDisposable 时只需担心确定性内存管理
。只要您遵循使用
块的最佳实践,通常就没有问题。You're looking at this in perhaps the wrong way:
Then your next step:
The GC will come along and "collect" any of these objects which have no chain of references to a live object at some point in the future. For most tasks, as long as you're using managed data, you should not worry about large objects versus small objects.
For most tasks you only worry about deterministic memory management when dealing with
IDisposable
. As long as you follow the best practice ofusing
-blocks, you will generally be fine.您创建一个新对象并将对其的引用分配给变量
c
。同时,先前的对象(“红色汽车”)现在不再被引用,并且可能被垃圾收集。You create a new object and assign a reference to it to your variable
c
. At the same time the previous object (the "red car") is now not referenced anymore and may be garbage collected.如果没有其他对红色汽车的引用,GC 将在下一个周期收集它。您不需要任何额外的东西(除非它是一个具有需要处理的流等的类)
If there are no other references to Red car, it will be collected by the GC on its next cycle. You don't need anything extra (unless it's a class that has streams etc. that need to be disposed)
当红色汽车不再有根(无法到达)时,垃圾收集器将处理红色汽车的清理工作。作为开发人员,您通常不必担心清理 .Net 中的内存。
需要提及三个警告:
using
语句是实现此目的的好方法。The garbage collector will handle cleanup for the red car when it is not longer rooted (not reachable). You, the developer, don't generally have to worry about cleaning up memory in .Net.
There are three caveats that need to be mentioned:
using
statement is a good way to accomplish this.垃圾收集器将负责处理汽车对象
Garbage collector will take care of disposing of Car object
GC 将拾取您的
Red Car
对象并丢弃它。如果原始对象不再使用时需要释放资源,则可以调用自定义析构函数或实现 IDisposable。
The GC will pick up your
Red Car
object and dispose of it.You can call a custom destructor or implement IDisposable if you have resources that need to be released when the original object is no longer used.
如果
Car
拥有一些本机资源,您需要实现IDisposable
并在重用变量之前正确处置它。In case
Car
holds some native resources you'll want to implementIDisposable
and dispose of it properly before reusing the variable.我认为你应该实现 IDispose 接口来清理非托管资源
I think you should implement the IDispose interface to clean up unmanaged resources