Separating managed code from unmanaged code RRS feed

  • Question

  • I have 1000 lines of code with me. i would like to figure out how many lines of code  runs under CLR ( i mean managed code) and how many lines of code doesn't ( unmanged code ). Is there any tool available to figure out this? .
    Eventhough we can figure out the unmanged code manuallly ( like database connections and open file handles etc )  it's troublesome to go thru each and every line figure out unmanaged code and write the dispose/Finally stuff.

    Also is there any way that we can find when GC starts executing ( i know the process is undeterministic but is there any way ( like showing a MessageBox) when GC is about to start collectiong unused references.
    Friday, October 10, 2008 10:23 AM


  • This is not how it works.  At the machine level, both managed code and unmanaged code is just plain machine code.  In case of managed code, that machine code is produced by the JIT compiler.  A thread may be started by unmanaged code and it may, through a callback, enter machine code that was produced by the JITter.  The other way around is even more common, a large number of .NET class methods end up executing some kind of Windows provided unmanaged code.

    Deciding whether you need to implement IDisposable or a finalizer is pretty straight forward.  A finalizer is *very* uncommon, you'd have make a P/Invoke call to grab a kernel resource.  You should leave that job to a .NET class, it has a finalizer to release the resource.  When it has a finalizer, it always has a Dispose() method.  When you add a class member of a type that implements Dispose(), you must implement Dispose() as well.  Just call the member's Dispose() method.

    You cannot find out when GC starts, it runs concurrently with your own threads.  One exception: if you use the <gcServer> element in the app.config file, you can get a notification when you use the .NET V3.5 SP1 version of the framework.

    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Thursday, October 16, 2008 5:35 AM
    • Marked as answer by Zhi-Xin Ye Thursday, October 16, 2008 5:35 AM
    Friday, October 10, 2008 12:07 PM