I was wondering how much memory does an object that inherits from "object" and has no fields/properties take ? And I guess methods don't. Right ? I am talking for .net objects.
- 
                    Good question. I'd say create a few million of them and see the memory difference before and after. Unless someone's already done this. – Michael Todd Aug 13 '09 at 16:24
- 
                    Thats why I asked. I needed to know the memory usage of application with millions of objects. So the answer for 32 bit app is the size of all fields +8 bytes. – George Statis Aug 13 '09 at 17:28
3 Answers
Okay, as both Andrew and Guffa have given answers which I believe to be wrong...
There's an 8 byte overhead for all objects (on x86), but there's also a minimum size of 12 bytes. I don't know why... but it means that these two classes both take 12 bytes per instance:
public class OneField
{
    private int field;
}
public class NoFields
{
}
Test:
using System;
public class OneField
{
    private int field;
}
public class NoFields {}
public class Test
{
    static void Main(string[] args)
    {
        int size = int.Parse(args[0]);
        switch (args[1])
        {
            case "NoFields":
                TestNoFields(size);
                break;
            case "OneField":
                TestOneField(size);
                break;
        }
    }
    static void TestNoFields(int size)
    {
        NoFields[] array = new NoFields[size];
        long start = GC.GetTotalMemory(true);
        for (int i=0; i < size; i++)
        {
            array[i] = new NoFields();
        }
        long end = GC.GetTotalMemory(true);
        GC.KeepAlive(array);
        Console.WriteLine("Size per instance: {0}",
                          (end-start) / (double)size);
    }
    static void TestOneField(int size)
    {
        OneField[] array = new OneField[size];
        long start = GC.GetTotalMemory(true);
        for (int i=0; i < size; i++)
        {
            array[i] = new OneField();
        }
        long end = GC.GetTotalMemory(true);
        GC.KeepAlive(array);
        Console.WriteLine("Size per instance: {0}",
                          (end-start) / (double)size);
    }
}
This is ugly because I've deliberately not gone for any generic types or anything else that could cause issues. A few test runs:
>test 1000000 NoFields
Size per instance: 12.000024
>test 1000000 OneField
Size per instance: 12.000024
>test 1000 NoFields
Size per instance: 12
>test 1000 OneField
Size per instance: 12
(JITting overhead etc explains why the number isn't always an exact integer - hence why I do the division in floating point.)
Testing with an extra int field shows the usage going up to 16, which proves it is actually doing something sensible :)
 
    
    - 1,421,763
- 867
- 9,128
- 9,194
- 
                    Good point about the 12 byte minimum but I do think that the spirit of the question pertained to exactly what information is "inherited" from `System.Object`. You are correct though that on x86, the first field is "free" so to speak but without the overhead of `System.Object`, the first 3 fields could have been free :) Still, +1 to you for this important distinction. – Andrew Hare Aug 13 '09 at 16:37
An object has two references/pointers additional to it's own data.
So, on a 32 bit system the object would take 8 bytes, on a 64 bit system it would take 16 bytes.
Correction:
As Jon stated, the minimum size for an object is 12 bytes. The information that I found so far says that the GC requires this.
 
    
    - 687,336
- 108
- 737
- 1,005
- 
                    4Nope - there's a minimum object size of 12 bytes on x86, for some reason, but you get the first field "free" :) – Jon Skeet Aug 13 '09 at 16:32
- 
                    According to this http://msdn.microsoft.com/en-us/magazine/cc163791.aspx the GC requires the object to be at least 12 bytes for some reason. – Guffa Aug 13 '09 at 16:39
The only overhead that you would incur with a reference type would be 4 bytes for the type object pointer and 4 bytes for the sync block index.
So in total, 8 bytes of overhead.
 
    
    - 344,730
- 71
- 640
- 635
