TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# Array Versus List Memory Usage

Test the memory usage of arrays and Lists. Arrays can be used in a more efficient way.
Arrays, memory. Arrays are memory-efficient. Lists are built on top of arrays. Because of this, Lists use more memory to store the same data.
We provide a detailed comparison of array and List memory usage. A simple benchmark can be used to measure the memory of lists and arrays.
We focus on how the 2 data structures perform. The 2 code examples contrast how you can use an array with more complex logic, and a List with simpler logic.

Here: The List collection is built up at runtime. It may have to allocate or change the positions in memory during garbage collection.

List Add

Note: The int array is declared and created in one statement. Thus it will store all values in neighboring memory.

Int Array
C# program that uses List using System.Collections.Generic; class Program { static void Main() { // Compare time to build up a List. List<int> list = new List<int>(); for (int i = 0; i < 60000; i++) { list.Add(i); } } } C# program that uses array using System.Collections.Generic; class Program { static void Main() { // Compare time to allocate an array and assign to it. int[] array = new int[60000]; for (int i = 0; i < 60000; i++) { array[i] = i; } } } Memory usage for arrays and Lists: List generic: 6.172 MB Integer array: 5.554 MB Lookup performance for arrays and Lists: List generic: 1043.4 ms Integer array: 980.2 ms
Discussion. Arrays and Lists can change performance and memory usage. The benchmarks were taken from a more complex program, but they show the pattern of arrays being more efficient.

And: Arrays were about 7% faster, even when other code is involved. The usage was with a data structure that looks up items.

Notes, memory. Regarding memory usage, the int array was more compact than the List generic. There can be substantial overhead when using generics instead of arrays.
Notes, generics. You might be able to convert from generics to arrays. You could store the size of the array and use it when initializing later. That way you don't have to resize anything.Generic Class, Method

Tip: These findings are only useful as a hint of how you can influence performance by changing from a List to an array.

Tip 2: This article can tell us that sometimes you can optimize by changing a List to an array.

A summary. We saw a comparison of List and array memory usage in the C# language. For speed it is sometimes worthwhile to prefer regular arrays. The performance benefit is significant.Optimization
© TheDeveloperBlog.com
The Dev Codes

Related Links:


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