Get REAL memory usage for a type in C#

So far, the using direct IL is very seldom required with how powerful C# is. However, I found a good need: C#'s sizeof operator sucks!

So, I wrote a tiny library to use the sizeof IL instruction.


  • Doesn't throw an error on reference types(returns size of pointer for platform)
  • Accurately gets the size of an object IN MEMORY, not just the used size of the type.
  • Has no problems dealing with mixed structs containing references and fields
  • Works exactly how you would expect it to and will work on any type

Also, the IL is extremely simple, boiling down to 2 actual instructions sizeof and ret. It also is verifiable and runs on Mono and .Net, and of course works on any platform (probably including Mono For Android and friends and compact framework?)

It's BSD licensed and here is some example code showing how to use it:

using System;

using Earlz.BareMetal;
namespace Testfoo
  class MainClass
    public static void Main (string[] args)
  struct Foo
    int a, b;
    byte c;
    object foo;
  class Bar
    string foo;
    string bar;
    AssemblyLoadEventArgs meh;
    DateTime d;

You can get the precompiled library (and see the source) at bitbucket

Posted: 11/14/2012 3:17:15 AM

ILDump -- New mini project

I've created a new little project now which hopefully fill a small niche.

Basically, at my work I'm currently debugging an issue where subtle IL changes(such as adding a nop or slightly reordering a switch) exposes a bug in Microsoft's JIT compiler for .Net 4.5. I have two assemblies. One that works, and one that crashes. The only difference between them is subtle IL changes. They both do the same thing, have the same methods, etc.. and they work fine on a JIT compiler that isn't 4.5.

So far, my attempts at figuring out what's going on has been going very poorly. The main reason is that there aren't a ton of tools out there for working with IL. I know, I could dump the IL as C# or something with ILSpy, but the IL is so similar that it turns into the same C# code. Attempts to reproduce it in a smaller test case have went poorly so far as well.

My options were as such:

  1. Use ILDasm and then a diff program to figure out the difference between the programs
  2. Use Reflector to dump out IL like ILDasm, but also put it in separate files.
  3. Pound my head against the wall

So far, option 3 has been showing the most promise. The problem with dumping IL and then trying to diff it is that current disassemblers will prefix a label for the address of every line... this is fine except for when you prefer that every line doesn't have a difference when a nop is added that changes the address.

So, how does ILDump solve this problem? Well, first off it won't print out labels on each line unless another instruction "points" to that label. Also, because addresses don't stay constant, it can rename labels to something like Label1 instead of IL_0012, to make less false differences. Also, it includes a small option for trimming out NOPs, sorting by method name, and constraining it's output to a class/namespace.

Anyway, ILDump is BSD licensed over at BitBucket. It uses Mono.Cecil for it's IL parsing, and compiles/works on Mono and Microsoft .Net. Also, if you want to download a precompiled version, try out

It's a very simple tool, so there hasn't been a whole lot to do on it, but it's still improving by the day(hour).

Posted: 9/2/2012 6:57:05 PM