1

I wanted to run out of the stack memory (1 Mb) by creating this:

List<int> list = new List<int>(262144+1);

However I didn't receive any exceptions, though the Visual Studio memory profiler says I'm using 9 Mb of memory. So where was this memory allocated?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    @derloopkat Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM . – Linda Lawton - DaImTo Jun 14 '17 at 09:55
  • 2
    I think your `List` was not allocated to stack, but to heap memory. take a look at https://stackoverflow.com/questions/4102547/i-want-to-allocate-an-object-on-the-stack-with-c-sharp – Timothy Groote Jun 14 '17 at 09:55
  • Arrays are reference types, their storage is always allocated on the GC heap. Using stack-allocated memory is only possible by using the unsafe `stackalloc` keyword. – Hans Passant Jun 14 '17 at 11:47

0 Answers0