销毁C#中的结构对象? [英] Destroying a struct object in C#?

查看:178
本文介绍了销毁C#中的结构对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我担心的是,在C#中只引用类型得到垃圾回收有点糊涂了。
这意味着GC挑选只有参考类型的内存取消分配。
那么与值类型发生,因为他们也占用内存堆栈?

I am a bit confused about the fact that in C# only the reference types get garbage collected. That means GC picks only the reference types for memory de-allocation. So what happens with the value types as they also occupy memory on stack ?

推荐答案

一开始,他们是否再堆的堆或部分取决于什么情况下他们的一部分 - 如果他们是引用类型之内,他们将在堆上反正。 (你应该考虑多少你真的关心栈/堆鸿沟无论如何 - 作为埃里克利珀写,这是的基本上实现细节。)

For a start, whether they're on the stack or part of the heap depends on what context they're part of - if they're within a reference type, they'll be on the heap anyway. (You should consider how much you really care about the stack/heap divide anyway - as Eric Lippert has written, it's largely an implementation detail.)

不过,基本值类型的内存回收时上下文被回收 - 所以当栈是由你从方法返回弹出,认为回收整个堆栈帧。同样,如果值类型值实际上是对象的一部分,则当垃圾收集的对象的内存被回收。

However, basically value type memory is reclaimed when the context is reclaimed - so when the stack is popped by you returning from a method, that "reclaims" the whole stack frame. Likewise if the value type value is actually part of an object, then the memory is reclaimed when that object is garbage collected.

简短的回答是,你不必担心它:)(这是假设你没有任何的其他的比内存的后顾之忧,当然 - 如果你已经有了结构与需要发布到本机的手柄引用,这是一个有所不同的场景。)

The short answer is that you don't have to worry about it :) (This assumes you don't have anything other than the memory to worry about, of course - if you've got structs with references to native handles that need releasing, that's a somewhat different scenario.)

这篇关于销毁C#中的结构对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆