C# 的“新”现有对象的关键字

发布于 2024-11-24 07:20:28 字数 241 浏览 1 评论 0原文

我想知道一旦对象的引用被重新分配,它会发生什么(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

泼猴你往哪里跑 2024-12-01 07:20:28

在您的示例中,当将 c 分配给 Blue Car 时,cRed Car 实例将有资格进行垃圾回收>。你不需要做任何事情。

查看这篇关于 .NET 垃圾收集器的(旧的,但仍然相关)MSDN 文章。 http:// msdn.microsoft.com/en-us/magazine/bb985010.aspx

第一段说明了一切:

Microsoft .NET 公共语言运行时环境中的垃圾收集完全使开发人员无需跟踪内存使用情况并了解何时释放内存。

In your example, the Red Car instance of c will become eligible for garbage collection when c is assigned to Blue 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:

Garbage collection in the Microsoft .NET common language runtime environment completely absolves the developer from tracking memory usage and knowing when to free memory.

风苍溪 2024-12-01 07:20:28

由于引用被重用,垃圾收集器在“红色汽车”丢失引用后是否会处理/处理它?<​​/p>

您可能以错误的方式看待这个问题:

c [*] ----> [Car { Name = "Red Car" }]  // Car c = new Car("Red Car")

然后您的下一步:

c [*]       [Car { Name = "Red Car"  }] // No chain of references to this object
   \------> [Car { Name = "Blue Car" }] // c = new Car("Blue Car")

GC 将出现并“收集”任何具有 在未来的某个时刻没有对活动对象的引用链。对于大多数任务,只要您使用托管数据,您就不必担心大对象与小对象。

对于大多数任务,您在处理 IDisposable 时只需担心确定性内存管理。只要您遵循使用块的最佳实践,通常就没有问题。

Since the reference was reused, does the garbage collector dispose / handle the 'Red Car' after it's lost it's reference?

You're looking at this in perhaps the wrong way:

c [*] ----> [Car { Name = "Red Car" }]  // Car c = new Car("Red Car")

Then your next step:

c [*]       [Car { Name = "Red Car"  }] // No chain of references to this object
   \------> [Car { Name = "Blue Car" }] // c = new Car("Blue Car")

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 of using-blocks, you will generally be fine.

甜中书 2024-12-01 07:20:28

您创建一个新对象并将对其的引用分配给变量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.

没有伤那来痛 2024-12-01 07:20:28

如果没有其他对红色汽车的引用,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)

洋洋洒洒 2024-12-01 07:20:28

当红色汽车不再有根(无法到达)时,垃圾收集器将处理红色汽车的清理工作。作为开发人员,您通常不必担心清理 .Net 中的内存。

需要提及三个警告:

  1. 它不会立即发生。当垃圾收集发生时就会发生。你无法预测它。
  2. 如果该类型实现 IDisposable,则由您来确保调用 .Dispose() 方法。 using 语句是实现此目的的好方法。
  3. 你提到它是一个大物体。如果超过 85000 字节,它将存储在一个称为“大对象堆”的地方,该堆的垃圾收集规则非常不同。允许频繁回收此类对象可能会导致问题。

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:

  1. It won't happen right away. Garbage collection will happen when it happens. You can't predict it.
  2. If the type implements IDisposable, it's up to you to make sure the .Dispose() method is called. A using statement is a good way to accomplish this.
  3. You mentioned it's a large object. If it's more than 85000 bytes it will stored in a place called the Large Object Heap, which has very different rules for garbage collection. Allowing this kind of object to be recycled frequently can cause problems.
酷炫老祖宗 2024-12-01 07:20:28

垃圾收集器将负责处理汽车对象

Garbage collector will take care of disposing of Car object

淡淡绿茶香 2024-12-01 07:20:28

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.

獨角戲 2024-12-01 07:20:28

如果 Car 拥有一些本机资源,您需要实现 IDisposable 并在重用变量之前正确处置它。

In case Car holds some native resources you'll want to implement IDisposable and dispose of it properly before reusing the variable.

红墙和绿瓦 2024-12-01 07:20:28

我认为你应该实现 IDispose 接口来清理非托管资源

public class car : IDispose 

I think you should implement the IDispose interface to clean up unmanaged resources

public class car : IDispose 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文