0

I've gotten used to the memory management of C++ and having to consider if data should be stored in the Stack or Heap though coming back to C#, I'm a little confused on if this should really be a concern anymore.

I'm aware that C# uses a Garbage Collector but does that mean I still need to allocate data to either the Stack or Heap? I mainly ask this considering I'm using the new keyword a lot more then I would in C++

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
codelyoko373
  • 141
  • 1
  • 9
  • No, the runtime handles it all for you. – stuartd Sep 05 '17 at 00:15
  • 1
    In almost 20 years of coding for .NET I've only had to worry about it a couple of times. Those times were highly-specific cases where I needed to load massive amounts of data into memory at one time (multi-gigabytes). This is not a usual scenario. – Sam Axe Sep 05 '17 at 00:17
  • 1
    It is, in fact, much more interesting than that in that you should probably *unlearn* what you think you know about how the stack and the heap are used, because things work differently in the managed world, even ignoring the garbage collector for a moment. [Take it away, Eric](https://blogs.msdn.microsoft.com/ericlippert/2009/04/27/the-stack-is-an-implementation-detail-part-one/). – Jeroen Mostert Sep 05 '17 at 00:23
  • See also https://stackoverflow.com/questions/4487289/memory-allocation-stack-vs-heap. Note that in general, you'll have little control over where your data is stored, because the _semantics_ of your data types will determine the storage location implicitly, and will be much more important than the underlying implementation detail of _how_ the data is stored. – Peter Duniho Sep 05 '17 at 00:23
  • Yes, you really should worry. The stack is a nice organised data structure where everything is neat. The heap is just a morass of items where I can never find anything. Use the stack. :-) – paxdiablo Sep 05 '17 at 01:18

0 Answers0