Destroying a struct object in C#?

2020-02-10 11:27发布

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 ?

8条回答
一纸荒年 Trace。
2楼-- · 2020-02-10 12:05

value types would get deallocated when the stack frame is removed after it has been executed i would assume

查看更多
冷血范
3楼-- · 2020-02-10 12:12

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.)

查看更多
狗以群分
4楼-- · 2020-02-10 12:18

Would also like to add that stack is at a thread level, and heap is at application domain level.

So, when a thread ends it would reclam the stack memory used by that specific thread.

查看更多
▲ chillily
5楼-- · 2020-02-10 12:20

Value types are destroyed as soon as they go out of scope.

查看更多
【Aperson】
6楼-- · 2020-02-10 12:21

A value type on the stack is removed from the stack when it goes out of scope.

查看更多
▲ chillily
7楼-- · 2020-02-10 12:28

There are too many verbs used in this question, like destroyed, reclaimed, deallocated, removed. That doesn't correspond well with what actually happens. A local variable simply ceases to be, Norwegian parrot style.

A method has a single point of entry, first thing that happens is that the CPU stack pointer is adjusted. Creating a "stack frame", storage space for the local variables. The CLR guarantees that this space is initialized to 0, not otherwise a feature you use strongly in C# because of the definite assignment rule.

A method has a single point of exit, even if you method code is peppered with multiple return statements. At that point, the stack pointer is simply restored to its original value. In effect it "forgets" that the local variables where ever there. Their values are not 'scrubbed' in any way, the bytes are still there. But they won't last long, the next call in your program are going to overwrite them again. The CLR zero-initialization rule ensures that you can never observe those old values, that would be insecure.

Very, very fast, takes no more than a single processor cycle. A visible side-effect of this behavior in the C# language is that value types cannot have a finalizer. Ensuring no extra work has to be done.

查看更多
登录 后发表回答