C# Jagged Versus 2D Array Memory

Two-dimensional (2D)

Jagged arrays have different memory usage characteristics. They require more objects but sometimes fewer elements than 2D arrays. By comparing the memory allocated for these types in the .NET Framework, we determine how they are represented in the managed heap as an object model.

Key points: 2D arrays use less memory than rectangular jagged arrays. 2D arrays require fewer objects on the managed heap. Jagged arrays are more flexible.

Example

Note

This program written in the C# language tests the memory usage of two 1000 x 100 arrays. The first program version uses a jagged array to represent the integer data. It must allocate 1001 separate arrays. The second program uses a two-dimensional array to represent the data. It only allocates one object on the managed heap for this.

Program that tests jagged array memory [C#]

using System;

class Program
{
    static void Main()
    {
	long b1 = GC.GetTotalMemory(true);
	//
	// This program tests the memory usage of a 1000 x 100 element jagged array.
	//
	int[][] jagged = new int[1000][];
	for (int i = 0; i < 1000; i++)
	{
	    jagged[i] = new int[100];
	}
	long b2 = GC.GetTotalMemory(true);
	jagged[0][0] = 0;
	Console.WriteLine("{0} bytes (jagged 1000 x 100)", b2 - b1);
    }
}

Output

416028 bytes (jagged 1000 x 100)

Program that tests 2D array memory [C#]

using System;

class Program
{
    static void Main()
    {
	long b1 = GC.GetTotalMemory(true);
	//
	// This tests the memory usage of a 1000 x 100 element two-dimensional array.
	//
	int[,] array = new int[1000, 100];
	long b2 = GC.GetTotalMemory(true);
	array[0, 0] = 0;
	Console.WriteLine("{0} bytes (2D 1000 x 100)", b2 - b1);
    }
}

Output

400032 bytes (2D 1000 x 100)

Testing memory usage of jagged array. The first program in this article uses the GC.GetTotalMemory method to acquire the size in bytes of the managed heap before and after a jagged array of 1000 subarrays is allocated. The program ensures that the entire array is not optimized out by assigning to it after the second memory measurement. The program prints out the size of the managed heap with the jagged array allocated.

Testing memory usage of 2D array. The second program also uses the GC.GetTotalMemory method to measure the size of the managed heap. This program allocates a two-dimensional array of size 1000 x 100, which is the same number of elements as the jagged array. It prints out the size in bytes of the entire program during its execution as well.

Results. The results of this experiment are that the jagged array requires 15,996 more bytes to represent the integer data than the 2D array. Please note that the 2D array is more limited in what shapes of data it can represent, and this enables the size savings.

Irregular subarray lengths

Programming tip

Although a rectangular two-dimensional array uses less memory than a rectangular jagged array, an array that is not truly rectangular and has varying subarray lengths could use less memory as a jagged array. However, the cost of having more array objects on the heap will always be present with a jagged array. With very small subarray length variations, the 2D array could still be smaller in memory usage.

Performance

Performance optimization

In the .NET Framework, jagged arrays are faster to access elements in because of optimizations in the runtime for one-dimensional arrays, which jagged arrays are made out of. Accessing elements in multidimensional arrays is slower. Allocating two-dimensional arrays is faster, however, because only one object must be created to store all the references or values.

Also, a jagged array could slow down garbage collection because more objects are on the managed heap. If a part of the program is not frequently used and access time is not critical, the 2D array might be a better choice because it requires fewer objects on the heap for the runtime to manage.

Summary

The C# programming language

We looked at the memory usage of complex array types in the C# programming language, first measuring the bytes allocated for a rectangular jagged array and then for a two-dimensional array. We saw that for this 1000 x 100 integer data, the two-dimensional array required fewer object allocations and also required less memory. The two-dimensional array will result in less work for the garbage collector, but can be slower to access elements in.

Array Types
.NET