TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Jagged Versus 2D Array Memory

This C# performance article tests how much memory jagged and 2D arrays use.

Jagged arrays have different memory usage characteristics.

They require more objects but sometimes fewer elements than 2D arrays. We compare the memory allocated for these types in the .NET Framework.

Note: 2D arrays use less memory than rectangular jagged arrays.
2D arrays require fewer objects on the managed heap.

Note 2: Jagged arrays are more flexible. They are faster in some program contexts.

Jagged Arrays

Example. This program written in the C# language tests the memory usage of two 1000 by 100 arrays. The first program version uses a jagged array to represent the integer data. It must allocate 1001 separate arrays.

And: The second program uses a two-dimensional array to represent the data. It only allocates one object on the managed heap for this.

C# program that tests jagged array memory

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)

C# program that tests 2D array memory

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)

The first program uses GC.GetTotalMemory to get 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.

Then: The program prints out the size of the managed heap with the jagged array allocated.

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. This is the same number of elements as the jagged array.

Then: It prints out the size in bytes of the entire program during its execution as well.

In this experiment, 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.

Discussion. A rectangular two-dimensional array requires less memory than a rectangular jagged array. But 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.

And: With small subarray length variations, the 2D array could still be smaller in memory usage.

Performance. In the .NET Framework, jagged arrays have better element access performance. The runtime optimizes one-dimensional arrays. And jagged arrays are made out of one-dimensional arrays. Accessing elements in multidimensional arrays is slower.

But: Allocating two-dimensional arrays is faster. 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 rarely used and access time is not critical, the 2D array might be a better choice.

And: The 2D array requires fewer objects on the heap for the runtime to manage. This makes GC faster.

Summary. We measured the bytes allocated for a rectangular jagged array and then for a two-dimensional array. For this 1000 x 100 integer data, the two-dimensional array required fewer object allocations and less memory.

Thus: The two-dimensional array will result in less work for the garbage collector, but can be slower when accessing elements.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf