Alex Rivera | Logout

What exactly is "managed" code?

Asked 2008-09-11T23:38:58.157
57

I've been writing C / C++ code for almost twenty years, and I know Perl, Python, PHP, and some Java as well, and I'm teaching myself JavaScript. But I've never done any .NET, VB, or C# stuff. What exactly does managed code mean?

Wikipedia describes it simply as

Code that executes under the management of a virtual machine

and it specifically says that Java is (usually) managed code, so

  • why does the term only seem to apply to C# / .NET?
  • Can you compile C# into a .exe that contains the VM as well, or do you have to package it up and give it to another .exe (a la java)?

In a similar vein,

  • is .NET a language or a framework, and what exactly does "framework" mean here?

OK, so that's more than one question, but for someone who's been in the industry as long as I have, I'm feeling rather N00B-ish right now...

Edit
Report

2 Answers

11

Mostly its referring to the fact that all of your memory allocations are "managed" for you. If you are using managed code you don't have to worry about freeing your objects when you are done with them. Simply allowing them to go out of scope will mean that the VM will eventually recognize that there are no longer any references to them and will Garbage collect them returning the memory to the system.

Unmanaged code on the other hand will simply "leak" unless you explicitly free your pointers before you discard the references.

answered 2008-09-11T23:46:41.757
6

Under .NET and Visual C++ specifically, you can have both Unmanaged and Managed code. The terms refer to the manner in which memory is allocated and 'managed'.

Unmanaged code would be the C++ stuff you're used to. Dynamic memory allocation and explicit freeing of the memory. The .NET runtime does not manage the memory for you, hence 'unmanaged'.

Managed code on the other hand IS managed by the run-time. You allocate memory where required (by declaring variables, not memory space) and the run-time garbage collector determines when it's no longer needed and cleans it all up. The garbage collector will also move memory around to improve efficiency. The run-time 'manages' it all for you.

As I mentioned above, it is possible to write code that is both managed and unmanaged.

Unmanaged:

class Bar : public Foo {
    private:
            int fubar;
    public:
            Bar(int i) : fubar(i) {}
            int * getFubar() { return * fubar; }
}

Managed:

public ref class Bar :  public Foo
    private:
            int fubar;
    public:
            Bar(int i) : fubar(i) {}
            int ^ getFubar() { return ^ fubar; }
}

Notice the ref? That pretty much designates a Managed class. It gets very confusing when you mix the two kinds of code however. For instance, you want to save a reference pointer, (^) the managed equivalent of a pointer, to a Picture Box control within your unmanaged class. Since the garbage collector can move memory around, the next time you try to dereference the picture box it can not be found. The run-time does not tell your unmanaged code about it's memory changes.

Therefore you need to pin down the managed object in memory to allow your unmanaged code to keep track of it. Then there's unboxing and all kinds of other quirks that allow you to intermix the two. Code complexity is enormous!

Officially, managed/unmanaged might come dow

answered 2008-09-20T13:22:42.327

Your Answer