Home All Groups Group Topic Archive Search About

How does managed code work?

Author
23 Jun 2009 6:39 PM
Siegfried Heintze
Would like to pursue some questions I recently encountered in an
interview -- someone might ask them again in another interview. I was not
very satisfied with the answers I gave the interviewer.

(1) Can someone point me to a discussion of the managed stack? Does it work
the same way as the native (CPU Vender implemented) stack with a frame
pointer that is the head of a linked list of stack frames where each time we
enter a function we create a new stack frame in which new variables are
pushed and each time we exit a function the entire stack frame is popped?

(2) Can someone point me to a discussion of the managed heap? How does it
work? Does it use counted pointers like COM often does? What, exactly,
happens when use operator= to (shallow) copy a SqlDataReader object from a
stack local variable to a global variable? How does it prevent memory leaks
that occur in COM when two objects reference each other and keep the others
reference count nonzero? How is the managed heap different than the native
heap? I think the managed heap implements defragmentation automatically like
Java. Does it use the mark and sweep algorithm or some other algorithm.

(3) Why do we have non-determanistic destructors in C#? Asked differently:
Why do some classes, like the SQL data reader, need to have their dispose
function called explicitly? Why did not the language designers implement
deterministic destructors so we would not have to manually use the "using"
statement or (worse yet) manually call the dispose function when the object
goes out of scope? Could not the language designers design the C# language
so the compiler tells runtime: "hey! this sqldatareader is going out of
scope so you better call the dispose function."

(4) Is there any circumstance where I would NOT want to call dispose
(explicitly or via the "using" statement) on function local SQL Data Reader
object when it is going out of scope?

(5) How is the structure of a managed DLL different from a native DLL?

(6) What choices of XML parser implementatoins do I have? I can call the
native MSXML via COM interop or PInvoke and I can the ones in System.Xml.
What is the difference? Is System.XML just a wrapper for MSXML?

(7) What choices of XML parser types are there? There is SAX and DOM. Any
other choices?

(8) What is the difference between using PInvoke to manipulate a semephore
or mutex and using System.Threading?

Thanks,
Siegfried

Author
24 Jun 2009 8:47 AM
Joe Fawcett
I'll start by answering the XML questions. System.Xml is not a wrapper for
the COM classes, it's completely new managed code. As well as DOM and Sax
(Rarely used in .NET) you also have the XmlReader which offers a pull model,
XPathDocument and the newer Linq to XML stuff (XElement etc.). If you really
want upset me, you could do things like load the XML into a DataSet and
manipulate from there.
The actual difference in usage between msxml2.DomDocument and
System.Xml.XmlDocument is not that great as they both try to implement the
standard DOM methods although XmlDocument has many more methods and
properties than the COM version.

--

Joe Fawcett (MVP - XML)

http://joe.fawcett.name

Show quoteHide quote
"Siegfried Heintze" <siegfr***@heintze.com> wrote in message
news:OSg%23uHD9JHA.3544@TK2MSFTNGP04.phx.gbl...
> Would like to pursue some questions I recently encountered in an
> interview -- someone might ask them again in another interview. I was not
> very satisfied with the answers I gave the interviewer.
>
> (1) Can someone point me to a discussion of the managed stack? Does it
> work the same way as the native (CPU Vender implemented) stack with a
> frame pointer that is the head of a linked list of stack frames where each
> time we enter a function we create a new stack frame in which new
> variables are pushed and each time we exit a function the entire stack
> frame is popped?
>
> (2) Can someone point me to a discussion of the managed heap? How does it
> work? Does it use counted pointers like COM often does? What, exactly,
> happens when use operator= to (shallow) copy a SqlDataReader object from a
> stack local variable to a global variable? How does it prevent memory
> leaks that occur in COM when two objects reference each other and keep the
> others reference count nonzero? How is the managed heap different than the
> native heap? I think the managed heap implements defragmentation
> automatically like Java. Does it use the mark and sweep algorithm or some
> other algorithm.
>
> (3) Why do we have non-determanistic destructors in C#? Asked differently:
> Why do some classes, like the SQL data reader, need to have their dispose
> function called explicitly? Why did not the language designers implement
> deterministic destructors so we would not have to manually use the "using"
> statement or (worse yet) manually call the dispose function when the
> object goes out of scope? Could not the language designers design the C#
> language so the compiler tells runtime: "hey! this sqldatareader is going
> out of scope so you better call the dispose function."
>
> (4) Is there any circumstance where I would NOT want to call dispose
> (explicitly or via the "using" statement) on function local SQL Data
> Reader object when it is going out of scope?
>
> (5) How is the structure of a managed DLL different from a native DLL?
>
> (6) What choices of XML parser implementatoins do I have? I can call the
> native MSXML via COM interop or PInvoke and I can the ones in System.Xml.
> What is the difference? Is System.XML just a wrapper for MSXML?
>
> (7) What choices of XML parser types are there? There is SAX and DOM. Any
> other choices?
>
> (8) What is the difference between using PInvoke to manipulate a semephore
> or mutex and using System.Threading?
>
> Thanks,
> Siegfried
>
Are all your drivers up to date? click for free checkup

Author
26 Jun 2009 8:56 PM
Ben Voigt [C++ MVP]
"Siegfried Heintze" <siegfr***@heintze.com> wrote in message
news:OSg#uHD9JHA.3544@TK2MSFTNGP04.phx.gbl...
> Would like to pursue some questions I recently encountered in an
> interview -- someone might ask them again in another interview. I was not
> very satisfied with the answers I gave the interviewer.

All these questions are quite reasonable.  I will answer your immediate
questions, but don't expect to do well in an interview without learning
through experience.

>
> (1) Can someone point me to a discussion of the managed stack? Does it
> work the same way as the native (CPU Vender implemented) stack with a
> frame pointer that is the head of a linked list of stack frames where each
> time we enter a function we create a new stack frame in which new
> variables are pushed and each time we exit a function the entire stack
> frame is popped?

It IS the native stack.  Managed code is converted into machine code by the
JIT compiler.  Just like with native code, the inlining optimization means
that there is not a stack frame created for every function call in the
source code.

>
> (2) Can someone point me to a discussion of the managed heap? How does it
> work? Does it use counted pointers like COM often does? What, exactly,
> happens when use operator= to (shallow) copy a SqlDataReader object from a
> stack local variable to a global variable? How does it prevent memory
> leaks that occur in COM when two objects reference each other and keep the
> others reference count nonzero? How is the managed heap different than the
> native heap? I think the managed heap implements defragmentation
> automatically like Java. Does it use the mark and sweep algorithm or some
> other algorithm.

The managed heap isn't really a heap at all, it's a stack.  The garbage
collector is generational.  At each collection of Gen0, objects that are
still reachable are moved onto the end of the Gen1 stack, and Gen0 is reset
to empty.  Reachability is determined using a few roots (static variable,
stack variables) so that mutual and circular references do not keep objects
alive.

Reference types aren't copied, just new references to them are made.  Value
types are copied bitwise.  So there are no user-defined copy constructors.

>
> (3) Why do we have non-determanistic destructors in C#? Asked differently:
> Why do some classes, like the SQL data reader, need to have their dispose
> function called explicitly? Why did not the language designers implement
> deterministic destructors so we would not have to manually use the "using"
> statement or (worse yet) manually call the dispose function when the
> object goes out of scope? Could not the language designers design the C#
> language so the compiler tells runtime: "hey! this sqldatareader is going
> out of scope so you better call the dispose function."

That design is definitely possible because C++/CLI does provide "stack
semantics" where Dispose is called when the reference goes out of scope.

>
> (4) Is there any circumstance where I would NOT want to call dispose
> (explicitly or via the "using" statement) on function local SQL Data
> Reader object when it is going out of scope?

Yes.  If you have another reference to that instance stored in a member
variable, or your return value.

>
> (5) How is the structure of a managed DLL different from a native DLL?

The managed DLL contains .NET metadata and MSIL code.  It also has an entry
for mscoree.dll in its import table, forcing the .NET runtime to load before
the managed "assembly".  The .NET runtime provides a JIT compiler which
converts the MSIL into native machine code.

>
> (6) What choices of XML parser implementatoins do I have? I can call the
> native MSXML via COM interop or PInvoke and I can the ones in System.Xml.
> What is the difference? Is System.XML just a wrapper for MSXML?

See Joe's answer.

>
> (7) What choices of XML parser types are there? There is SAX and DOM. Any
> other choices?

See Joe's answer.

>
> (8) What is the difference between using PInvoke to manipulate a semephore
> or mutex and using System.Threading?

System.Threading is a trusted library so the security checks are different.
P/Invoke requires an UnmanagedCode permission which is almost never
available in a partial trust scenarion.

Show quoteHide quote
>
> Thanks,
> Siegfried
>

Bookmark and Share