Jump to content
  • Advertisement
Sign in to follow this  
GraySnakeGenocide

Can someone give me the exact definition/usage of Stack/Heap and Reference Types in C#?

This topic is 2608 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have gone through a million books, tutorials, videos, researched. And it's all still just giving me a headache.

I've read that alot of books/etc don't give a GOOD representation of what is actually happening. Meaning I am just reading junk that isn't entirely true.

As for what I seemingly don't understand, it's the whole concept of stack/heap. All I know is that its about memory management, that is it.

As for Reference Types, I was in the process of learning about this and then the whole stack/heap thing came into play and just confused the hell out of me.

http://pastebin.com/7mn2394j

I somewhat get that X is holding a reference to MyInt. It's just that the whole stack/heap thing has my head spinning.

Share this post


Link to post
Share on other sites
Advertisement
Postscript: I've cleaned out some unhelpful stuff from this thread. Please keep it respectful, polite, and on-topic.

Thanks all!

Share this post


Link to post
Share on other sites
"The stack" is a section of a process's memory space that is used to hold local variables and information about the functions you are calling, such as their return address. It is so named because it operates like a traditional stack ADT. Local variables are pushed onto the stack when a function is called, and popped back off when the function returns. Memory accessed this way is relative to the currently executing function, and thus its address (or rather the relative offset) is known ahead of time. This becomes important later.

"The heap" is another section of memory that contains all other data allocated by the program. It is so named because some implementations use (or used) a heap data structure to organize the memory (though that's not required, which is why it also commonly referred to as the "free store"). The precise address of the allocation is not known in advance, since there is no rigid structure to the heap and memory addresses can change each time the program runs, which means the memory can only be accessed indirectly (such as through a pointer or a reference).

In C# (and the CLR), there are two different kinds of types: reference types and value types. Value types, as you may expect from their name, act like values. They directly contain their data in a continguous block of memory. When you have a local variable that's a value type, its data is placed directly onto the stack and the various function instructions operate on that memory directly. When they are a member of another type, their data is placed inline inside the memory block for that type.

Reference types, on the other hand, always store their memory on the heap. Since accessing memory on the heap is done indirectly, the actual reference type variable only stores an address to where the actual data can be found in memory. Local variables and members that are reference types only take up enough stack memory to hold that address, which will point to a block of heap memory that contains the actual type's data. If the data of the type happens to contain other value types (such as integers, characters, or user-created value types), those will also end up located on the heap, since value type memory is accessed directly and is placed inline wherever it is declared.

There is one further concept to understand. Since .NET supports a unified type system, meaning all types derive from a common base type (System.Object) which happens to be a reference type, there must be a mechanism to allow value types to be treated as reference types for the purposes of polymorphism. This mechanism is called boxing, and works by internally creating a reference type, similar to the MyInt type in your example, that will hold the value type. This boxed type can then be treated as a normal reference type, and later "unboxed" back into a value type by copying the memory indirectly refered to by the reference type directly into a new block of value type memory, either on the stack as a local variable or on the heap as part of another reference type. Note that since this is a copy, the data in the box does not reflect any changes made to the unboxed value type. They are in separate areas of memory.

Share this post


Link to post
Share on other sites
Thanks for the detailed explanations guys. I am slowly starting to understand it now (I'm not fully THERE yet). But after a few more readthroughs and such I guarantee I will start to get it. (I have a bad habit of having something stump the heck out of me, I give it a day or more, I come back, and it hits me like a ton of bricks.)

Once again, thanks guys.

Share this post


Link to post
Share on other sites
If I'm allowed to, I'd also like to thank the guys who took the time to write this all up -- this is a really clear explanation of the theory, and although I wasn't particularly searching for this topic, I learned something valuable today (and moreover, understood it!)

Thanks guys and OP for making this thread!

Share this post


Link to post
Share on other sites
The stack, when it comes to C#, is an implementation detail. It's a thing the JIT'ter uses to optimize your code and not much more.

There are some hard and fast ( and wrong ) rules about what goes on the stack, but more specifically the rules are actually what doesn't go on the stack. Namingly reference types and classes.

Value types *may* be created on the stack, as may structs, but there is actually no promise this will happen.


As to the super simple stupid explanation.


Ever been to a cafeteria where the plates are all stacked and when you go through the buffet you grab the top one? Well essentially that is what a stack is in C#. It's a chunk of memory that is set aside for ( the JIT engine to ) optimize access to certain types of variables. One of the advantages of a stack is, you know exactly where everything is, so there are no spaces and gaps and since we are dealing with value types, no unpredictability about when they will expire. This essentially means no garbage collection and very predictable locations, so no performance loss to searching or garbage collecting. This means, generally, the stack is always fastest.


Always fastest, but not always faster. Theoretically the heap can be just as fast, it all really depends on what goes on with memory. In the case of heap, instead of a stack, think of it like a giant array. As you allocate memory, "cells" of that array are used up. Eventually you use up all memory and one of two things occurs, the array is grown or garbage collection occurs, possibly both. Now as you are filling this array with memory, your performance is pretty much the same as the stack, as you are generally assigning your memory to the next top most location available.

Thing is, in time, just like your computer hardrive, this memory gets fragmented. So while the heap is continuous to start ( and therefore performing comparable to the stack ), as you allocate and free memory "holes" in the continuous array of memory locations essentially become available, so new allocations re-use old locations, or possibly are split into various different locations that have been made available. Now, when accessing your memory, you essentially don't have just a straight lookup any more, you may have multiple lookups and a search involved. Once this happens, the performance advantage of the stack becomes obvious.


Of course, as Eric Lippert said, this is all an implementation detail. You as the developer have absolutely no real control over what happens, beyond know what times explicitly will NOT be created on the stack.


The easiest ( and almost correct ) way to look at it is, the heap is memory as you generally think of it. The stack is a reserved piece of continuous memory reserved by the JIT engine for optimizing types that meet a certain criteria.




For a much more detailed explanation ( on a subject with a lot of misinformation ) from the man who is probably the second most knowledgeable person on C# read here and here.


It will basically tell you everything you needed to know.


I think a source of much of the confusion is the stack in C++, where the programmer had much more implicit control. In C# to be honest, I don't really know why they even made the distinguishment. They should have probably ignored the concept completely and made it something only the compiler developers were really aware of. Worst case scenario, for those few edge cases where the developer needed to optimize for stack usage, it could have been exposed as an attribute. Actually, there is already the stackalloc method, so even this wouldn't be need.

Share this post


Link to post
Share on other sites
I had to draw a picture of what the stack and the heap would look like after each line of assembly code (made by compiling some c code) was executed for a final one time.

Basically, the stack stuff goes away when the function goes out of scope - all of the variables it declares that don't use the static keyword disappear when it returns.

Stuff on the heap goes away when you release it (when you use delete or free()) or when the process gets judo chopped by the operating system.

Other than that there are some execution speed implications but the main rule is don't allocate and release stuff on the heap any more frequently than you have to (don't do it in loops or little functions that get called all the time).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!