Dot Net Perls

Memory Usage of Array and List - C#

by Sam Allen

Problem

Do arrays use less memory than generics? You want to look at benchmarks of Lists and arrays in real-world programs. Is it advantageous to use more complex code and logic to accomodate an array over List?

Solution: arrays and Lists in C#

Here we ignore practical differences and focus on how the two data structures perform. The two code examples below contrast how you can use an array with more complex logic, and a List with simpler logic.

using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Example t = new Example();
        t.A();
        t.B();
    }
}

class Example
{
    public void A()
    {
        // Compare time to build up a List individually.
        List<int> l = new List<int>();
        for (int i = 0; i < 60000; i++)
        {
            l.Add(i);
        }
    }


    public void B()
    {
        // Compare time to preallocate an array and assign to it.
        int[] s = new int[60000];
        for (int i = 0; i < 60000; i++)
        {
            s[i] = i;
        }
    }
}
  1. It builds up the List.
    The List is built up, one by one, at runtime. This means it may have to allocate more times or change the positions in memory during garbage collection. [C# - Garbage Collection - dotnetperls.com]
  2. It creates an array.
    The int[] array is declared and created all at once. Thus it will store all values in neighboring memory and won't need to go through any reallocations.

Information: benchmarking lookup time and memory usage

Here we see an example of how arrays and Lists can change performance and memory usage. The benchmarks here were taken from a more complex program, but they show the general pattern of arrays being more efficient.

What I found was that the arrays were about 7% faster, even when other code is involved. The usage was with a data structure that looks up items in an array or List. [C# - Directed Acyclic Word Graph - dotnetperls.com]

Regarding memory usage, the int[] array was considerably more compact than the List generic. There can be substantial overhead when using generics instead of arrays.

Question: should I always use arrays?

No. If you need the advanced features of generics, use them. However, 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.

Information: better performance data

This article is one of my earliest articles on the performance differences with arrays and Lists. There is detailed information on how arrays and MSIL works at this site. [Array Types and MSIL - dotnetperls.com]

Summary: memory usage of arrays and Lists

For speed, it's sometimes worthwhile to prefer regular arrays. The performance benefit is significant enough that changing your code may be worth your time, assuming the new version doesn't add too much complexity.

Dot Net Perls
About
Sitemap
Source code
RSS
Arrays
Sort String Array
List Element Equality
2D Array Use
Count Array Elements
Bind Array to DataSource
Recent
Pi
NGEN Installer Class
List Element Equality
DateTime Tips and Tricks
Remove HTML Tags From String
© 2008 Sam Allen. All rights reserved.