Search code examples
c#wpfsilverlightmemory-managementheap-memory

Is the memory that used by UserControl stores in Heap?


Possible Duplicate:
Fields of class, are they stored in the stack or heap?

I was reading some article about Stack vs Heap (just to refresh my knowledge).

Far as I know, if I create a value type inside a reference type (a class), the value type will also be stored in the Heap. Because where the value type being store is depends on where its created.

But then, when we create our application (say WPF/Silverlight), when we create our UserControl, ViewModel, isn't they are also a class (an object) a reference type?

So I kind of wonder, so no matter what I creates inside the UserControl and ViewModel will also be in Heap?


Solution

  • So I kind of wonder, so no matter what I creates inside the UserControl and ViewModel will also be in Heap?

    Yes, this is true. The UserControl is a class, so the memory for it, and any variables within it, it will be stored on the heap.

    There are two things here:

    1. This is purely an implementation detail. It doesn't/shouldn't really matter where the memory is stored. The usage semantics are all that really matter (ie: passing into a method just copies the reference, not the entire object, etc)
    2. The variable values may get copied into the stack as you use them. For details, see this (very detailed) answer by Eric Lippert detailing what happens with fields stored within a class.