if I have stack allocated value types what cleans them up? The GC is only heap right?
If the values are on the stack, then when the current stack frame is "popped" (by the method returning) the values are effectively cleaned up... there won't be any finalizers called or anything like that, but the memory used up by those values will be reused next time the stack gets that high.
Note that value types aren't always allocated on the stack, however. In particular, they're often allocated as part of other objects (e.g. a class with an int
member) or they can be boxed.
Stack allocated values disappear automatically with the stack frame they are contained in (i.e. when you return from the method).
stack works automatically, no need of explicit memory deallocation
精彩评论