Monday, March 17, 2014

Stack vs. Heap

To truly understand the stack and heap. It is helpful to fully look at the entirety of the memory allocation for any given process. And since there are several good sources for this information, in this article I hope to simply address the basics and summarize what others have previously explained.

Where in Memory is the Stack and Heap?
For a given process, a block of addresses are virtually assigned by the operating system for the stack, heap, data, and text of the code.

Per our diagram here is a summary of what each memory segment does:

  1. Text/Code: This is total instructions for running a process and does not change during the runtime. This is where your compiled code resides in memory (traditionally, this would be the assembly code as it was written for the application). It does contain the code for the functions, but is not where memory is stored for the runtime of the function. This segment will not change in size during the runtime of the process.
  2. Data/Global/Static: “Data” can be a confusing term for this segment since both the stack and the heap do contain in-memory data. However, this segment is specifically reserved for data that will be maintained for the lifetime of the process. In many programing languages this is known as global or static data. It should be noted that the primitive values, structs and object pointers are stored in this segment, but the dynamic data for an object is still stored in the heap (more on this later).
  3. Stack: This contains a series of stack frames. Each stack frame contains addresses for a function’s return value, arguments, local variables, and back-pointers to the previous function code.
  4. Heap: All object values that are referenced in the static and stack memory.

Ultimately, what is the difference between the Stack and the Heap?
I believe two fundamental statements should help clarify this:
  1. Primitive types (int, long, double, bool, etc) and structs (Guid, Size, Rectangle, etc) are stored directly on the stack.
  2. Object types are on the heap, but a variable’s (meaning the variable within the scope of the calling function) memory address pointing to a location on the heap is still on the stack.


public bool Foo(string a, Employee b, long c)
{
    int e = 2;
    Manager f = GetManager(a);

    return f.YearsWithCompany + e > c - b.YearsWithCompany; // Return value
}
* Note: Green is the stack. Blue is the heap.


This is a little over simplified, since a, b, and f are stored on the stack as variable addresses that point to their corresponding values stored on the heap. However, both c and e are values stored directly on the stack. Also, it should be noted that even though YearsWithCompany is an int (primitive) value it is still stored in the heap since it is value of the Manager and Employee objects.

References:
http://www.youtube.com/watch?v=_8-ht2AKyH4


http://stackoverflow.com/questions/203695/does-using-new-on-a-struct-allocate-it-on-the-heap-or-stack

7 comments:

  1. Thanks for your great and helpful presentation I like your good service.I always appreciate your post.That is very interesting I love reading and I am always searching for informative information like this.Well written article Thank You for Sharing with Us project management courses in chennai | pmp training class in chennai | pmp training fee | project management training certification | project management training in chennai | project management certification online |

    ReplyDelete

  2. Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing. iot course fees in chennai | iot certification courses in chennai | iot training institutes in chennai | industrial iot training chennai

    ReplyDelete
  3. Dr Itua cure my HIV, I have been a ARV Consumption for 10 years. i have been in pains until i came across Dr Itua on blogs site.I emailed him about my details of my HIV and my location i explained every thing to him and he told me that there is nothing to be scared of that he will cured me, he gave me guarantee,He ask me to pay for items fees so when i'm cured I will show gratitude I did and giving testimony of his healing herbs is what I'm going to do for the rest of you out there having HIV and other disease can see the good work of Dr Itua.I received his herbal medicine through EMS Courier service who delivered to my post office within 5 working days.Dr Itua is an honest man and I appreciate him for his good work.My GrandMa called him to appreciate him and rest of my friends did too,Is a joy to me that I'm free of taking Pills and having that fat belle is a nightmare.you will understand what i'm talking about if you have same problem I was having then not now though.I'm free and healthy Big Thanks To Dr Itua Herbal Center.I have his calendar too that he recently sent me,He Cure all kind disease Like,Cancer,Herpes,Fibromyalgia,Hiv,Hepatitis B,Liver/Kidney Inflamatory,Epilepsy,Infertility,Fibroid,Diabetes,Dercum,Copd ,and also Bring back Ex Lover Back..Here his Contact .drituaherbalcenter@gmail.com Or Whats_app Number +2348149277967

    ReplyDelete
  4. Outstanding blog thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.
    Check out : big data training and placement in chennai
    big data hadoop training in chennai
    big data certification in chennai
    hadoop big data training in chennai

    ReplyDelete
  5. Nice blog..! I really loved reading through this article. Thanks for sharing such a amazing post with us and keep blogging... best angularjs training institute in chennai | angularjs training in omr | angular 4 training in chennai | angularjs training in omr

    ReplyDelete
  6. Great information.
    It was very Useful.
    Keep sharing.
    Best Java training in bengaluru

    ReplyDelete