views:

179

answers:

3

I know that reference type will be garbage collected. I wanted to know whether value types will also be garbage collected from the stack?

+14  A: 

It is very unclear what your question means. Can you carefully define what "garbage collected" means? Does it mean "are inputs to the GC algorithm", or "are deallocated by compacting the GC heap", or what?

Values stored on the stack -- whether values of value types or reference types -- are the roots of the collection algorithm. They are not collected because they are the things that are alive that keep almost everything else alive.

And obviously they are not deallocated by compacting the GC heap; they're deallocated by popping the stack.

Does that answer your question?

UPDATE:

What I mean by "garbage collected" is that, if a value type variable is found not to be used by the application then it will be removed from the stack

OK, we're getting closer to an answerable question here I think. Now we need to understand what exactly you mean by "removed from the stack".

The stack is a block of pre-allocated memory one million bytes in size. Sometimes we use portions of that block of memory to store local variables of value type. What precisely do you mean by "removed from the stack"? The stack never changes in size; it's a one-million-byte block of pre-allocated memory.

The stack is divided into two contiguous regions, which we'll call the "valid" and "invalid" sections of the stack. On x86 architectures the ESP register points to the boundary between those regions. Are you asking "under what conditions does the memory associated with a particular local variable of value type on the stack become a part of the invalid section based on a change in value of the ESP register on x86 architectures?"

This might seem like a very, very "implementation detail" version of your question. The stack is an implementation detail of a specific version of the runtime, so if you're going to ask questions about it, you're going to have to accept the fact that you're asking about a specific value in a specific register on a specific chip architecture.

Further reading:

http://blogs.msdn.com/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx

http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx

http://blogs.msdn.com/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx

http://blogs.msdn.com/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx

I am a bit confused now to read what you have mentioned about "values" and "value types". I am finding it hard to understand the difference.

It is tricky! We use the words "value" and "reference" to mean too many things. Let me sum up.

A variable is a storage location.

Every variable has a type. A type can be a value type or a reference type.

A storage location contains a value.

The value of a variable of value type is a value of the value type. For example, int is a value type. The value of a variable of type int is an int, say, 12.

The value of a variable of reference type is a reference to an object of that type, or null. For example, string is a reference type. The value of a variable of type string is a reference to a string, or null.

That's why they're called "value types" and "reference types". The value of a value type is an instance of the type. The value of a reference type is a reference to an instance of the type.

Does that make sense now?

Eric Lippert
Thanks. This clears all my doubts :-). Thanks for this excellent explanation. (PS: I am a big fan of yours. I have impressed my friends by sharing the knowledge from your blogs!)
@csharpbaby: You're welcome. I've put some links to some blog articles you might find interesting in the text above.
Eric Lippert
+4  A: 

The common language runtime (CLR) allocates memory for objects in two places: the stack and the heap.

Value types are stored on the stack along with references to reference type content stored on the heap. The point of the garbage collector is to deallocate memory assigned to the reference type content on the heap when the reference is popped from the stack.

A value type that is not the content of a reference type is not stored on the heap so it is not cleaned up by the garbage collector.

See here for a slightly more in depth description of value vs reference types

See here for a lot more in depth description

Russell Giddings
Value types are not necessarily stored on the stack. A correct statement is "values of value type that are temporary storages or local variables that are not closed-over locals of an anonymous method or lambda expression and are not in an iterator block are stored on the stack in the Microsoft implementation of the CLI." A value type that is not on the GC heap is not cleaned up by the garbage collector. A value type that *is* on the GC heap *is* cleaned up by the garbage collector.
Eric Lippert
+1 Agreed, but trying to convert that in to a clear and concise answer that is easy to understand is a challenge ;-)
Russell Giddings
@Russell. You are right. I think I am thinking very hard now after Eric wrote "Values of value type". :)
I've just updated my second link, it has a really nice in depth explanation with lots of diagrams (which, if you're anything like me, help a lot!) :-)
Russell Giddings
A: 

Assuming you mean "garbage collected" in the sense of the garbage collector checking if they are alive and then reclaiming the memory then the answer is no they are not "garbage collected".

The reason is that memory on the stack is automatically reclaimed as stack frames are removed.

Have a read of this article: http://en.wikipedia.org/wiki/Stack_%28data_structure%29#Hardware_stacks

Paolo
Thanks Paolo. I will read this article :)