views:

490

answers:

3

When is memory allocated in the .NET compact framework? If there is a difference between value and reference types, please detail. Documentation or steps to confirm are appreciated.

In particular, consider this scenario...

private MyClass item;  // here?

public void MyMethod()
{
    item = new MyClass();  // or here?
}
+5  A: 

There are 3 different ways memory is allocated.

Static:

These are bound and allocated at compile time. Global static variables for example.

Stack Dynamic:

These are bound during runtime and pushed onto the stack. Such as a local variable in a function call.

Heap Dynamic:

Now heap dynamic also has a few different 'sub categories' such as implicit and explicit, but I won't go into that detail.

When you declare

private MyClass item;  // here?

a reference to MyClass is pushed onto the stack. It is only a reference and nothing more. Its value is null at that point.

public void MyMethod()
{
    item = new MyClass();  // or here?
}

It is at that point where memory is explicitly allocated on the heap by calling 'new MyClass()' and item then references it.

So in actuality, you have 2 variables after you call MyMethod. A refernce type named item, and an unnamed variable on the heap which item references that is of type MyClass.

Nicholas Mancuso
+1  A: 

This is something of a trick question, which makes me like it a lot more than I thought I would. :)

Let's start with the basics:

Declaring a variable does not directly allocate memory* for it on reference types. It does do so on value types, because value types are initialized at the point of declaration. For reference types, storage for the object is allocated at the point of entering their constructors.

*When does this not apply?

  • On the first usage of a given class (which may well be your first line), any class-level static data is initialized, and if the class has a static constructor (which presumably allocates memory), it will be called as well. Therefore, you may actually allocate some memory (for the static members) at a variable declaration.
  • Technically, declaring your reference type variable does allocate some memory, on the stack (as Nicholas Mancuso mentioned) -- the memory allocated is the stack-level storage for the reference to the object. (Admittedly, said reference is null, but once you initialize your object, a valid reference will exist in the allocated memory.)

I highly recommend this C-Sharp Corner article regarding stack and heap allocation for more information.

John Rudy
Very nice answer, adds a few things Nicholas left implied. I'll up vote, but he's getting the answer points. Thanks!
Anthony Mastrean
And he ought to; his answer was more thorough. :) He and I answered at almost the same time, off by just a minute or so. :)
John Rudy
A: 

I highly recommend that you look at the MSDN Webcase on Compact Framework Memory Management. It's not exactly the same as how it works on the desktop, and this talk covers it with a whole lot of detail and graphics to aid in understanding the GC Heap.

ctacke
The link is <https://msevents.microsoft.com/cui/WebCastRegistrationConfirmation.aspx?RegistrationID=1299365950>
Anthony Mastrean
But the event is over and I can't find an upload of the video or presentation.
Anthony Mastrean
Just register and it will give you a download link. It's not very intuitive.
ctacke