Is equivalent the memory used by an array of ints vs an array of structs having just one int?

Considering the next struct...

struct Cell
{
    int Value;
}

and the next matrix definitions

var MatrixOfInts = new int[1000,1000];
var MatrixOfCells = new Cell[1000,1000];

which one of the matrices will use less memory space? or are they equivalent (byte per byte)?

Upvotes: 6

Views: 113

Answers (3)

StackAlloc
StackAlloc

Reputation: 26

Both are the same size because structs are treated like any of the other value type and allocated in place in the heap.

long startMemorySize2 = GC.GetTotalMemory(true);
var MatrixOfCells = new Cell[1000, 1000];
long matrixOfCellSize = GC.GetTotalMemory(true);

long startMemorySize = GC.GetTotalMemory(true);
var MatrixOfInts = new int[1000, 1000];
long matrixOfIntSize = GC.GetTotalMemory(true);

Console.WriteLine("Int Matrix Size:{0}.  Cell Matrix Size:{1}", 
    matrixOfIntSize - startMemorySize, matrixOfCellSize - startMemorySize2);

Here's some fun reading from Jeffery Richter on how arrays are allocated http://msdn.microsoft.com/en-us/magazine/cc301755.aspx

Upvotes: 1

supercat
supercat

Reputation: 81149

In an array with value-type elements, all of the elements are required to be of the exact same type. The object holding the array needs to store information about the type of elements contained therein, but that information is only stored once per array, rather than once per element.

Note that because arrays receive special handling in the .NET Framework (compared to other collection types) arrays of a structure type will allow elements of the structures contained therein to be acted upon "in-place". As a consequence, if one can limit oneself to storing a structure within an array (rather than some other collection type) and can minimize unnecessary copying of struct instances, it is possible to operate efficiently with structures of almost any size. If one needs to hold a collection of things, each of which will have associated with it four Int64 values and four Int32 values (a total of 48 bytes), using an array of eight-element exposed-field structures may be more efficient and semantically cleaner than representing each thing using four elements from an Int64[] and four elements from an Int32[], or using an array of references to unshared mutable class objects.

Upvotes: 1

pgenfer
pgenfer

Reputation: 622

By using the sizeof operator in C# and executing the following code (under Mono 3.10.0) I get the following results:

    struct Cell
    {
        int Value;
    }

    public static void Main(string[] args)
    {
        unsafe
        {
            // result is: 4
            var intSize = sizeof(int);
            // result is: 4
            var structSize = sizeof(Cell);
        }
    }

So it looks like that an integer and a struct storing an integer consume the same amount of memory, I would therefore assume that arrays would also require an equal amount of memory.

Upvotes: 1

Related Questions