Variables are stored on the stack. The data it references can be on the stack (structs) or on the heap (classes). Structs also can be boxed, so objects on the heap are created. The garbage collector needs to free up unmanaged objects that are no longer needed from the managed heap. Using native APIs, memory can be allocated on the native heap. The garbage collector is not responsible for memory allocated on the native heap. You have to free this memory on your own. There’s a lot to consider with regard to memory.
When you use a managed environment, you can easily be misled to not pay attention to memory management because the garbage collector (GC) deals with that anyway. A lot of work is done by the GC; it’s very ...
Get Professional C# 7 and .NET Core 2.0, 7th Edition now with O’Reilly online learning.
O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.