Click here to Skip to main content
13,554,130 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 31 Mar 2009
Licenced CPOL

Generic List using LinkedList to overcome drawbacks of System.Collections.Generic.List

, 12 Apr 2009
Rate this:
Please Sign up or sign in to vote.
Generic List that can be used where loads of List items are added and there is too much of memory fragmentation using List


Implementing a Generic List like List to gain control over the allocation of List Items in the memory that is highly fragmented and no consecutive space to allocate a List.


I have 4GB of memory and 70% is free, and my web app throws SystemOutOfMemoryException. What the heck?? The stack trace pointed me out to the exact line of code where it all begin, the exception, and it was myList.AddRange(yourList); But I am just adding not more than 100 items, and myList never goes beyond 500 items for sure, there is enough memory available, then why "Out of Memory"??

I googled a lot, that came up with few articles from which I could find the reason but no proper solution to the problem. (Here is one that will brush you with some information.)

Then I thought of using LinkedList<T> instead of List<T>. Now I had a big trouble again, List<T> is so handy that I could easily add multiple items (AddRange()), I can traverse the list as I want and at any index (myList[i]), I can remove any of the Item I desire (RemoveAt(index)), which is a big pain in case of LinkedList<T>. So I decided to put a wrapper around LinkedList with all those functionalities provided by List<T>.

Using the code  

Attached Source code is simple to understand. A Class IAList defined, that contains an object as an internal storage, which is of type LinkedList<T>, which actually be used for storing our List Items linked.
public sealed class IAList<T> : IDisposable, IEnumerable, IEnumerable<T>
    LinkedList<T> internalStorage;

Note that, IAList<T> is implementing IDisposable, IEnumerable, IEnumerable<T> that gives ability to (1) dispose the IAList Object, where List<T> is not disposable, (2) to Enumerate (like in foreach). This class is also marked as serializable, so no worries when used where xml serializers are in action or binary formatter to create memory stream, the list is absolutely ready.

Implemented all those properties and methods (find in source code attached), that gives ability of List<T> to IAList<T>, like AddRange, RemoveAt, ElementAt, etc.

And also, this IAList is thread safe.

To get Enumerator out of IAList, I have defined a class Enumerator that implements IDisposable, IEnumerable, IEnumerable<T>

public sealed class IAList<T> : IDisposable, IEnumerable, IEnumerable<T>
    public sealed class Enumerator : IDisposable, IEnumerator, IEnumerator<T>
        T currentValue = default (T);
        LinkedList<T>.Enumerator storeEnumerator; 

Points of Interest 

There are many more drawbacks of using List, like, when you add range of elements, the list actually creates the copy of itself and appends the new elements and later frees up the old list, In this way, the List actually needs 3 times of its actual space when performed AddRange (or when performed bulk operations). Got to know how the List works, identified problem areas and fixed with a best alternative to the problem "SystemOutOfMemoryException"


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rahul D.
Technical Lead
India India
Acquired Degree of Bachelor in 2007 from Pune University, in VP College Of Engg., Baramati. There on worked in organisations to design and implement web based software applications.

You may also be interested in...


Comments and Discussions

GeneralLocks Pin
Jean-Paul Mikkers13-Apr-09 0:44
memberJean-Paul Mikkers13-Apr-09 0:44 
GeneralRe: Locks Pin
Rahul D.14-Apr-09 1:25
memberRahul D.14-Apr-09 1:25 
GeneralMy vote of 1 Pin
SlyLogin1-Apr-09 23:13
memberSlyLogin1-Apr-09 23:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 13 Apr 2009
Article Copyright 2009 by Rahul D.
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid