Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
12 Apr 2009CPOL2 min read 22.1K   2   4
Generic List that can be used where loads of List items are added and there is too much of memory fragmentation using List

Introduction 

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.

Background  

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.
C#
[Serializable]
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>

C#
   [Serializable]
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"

License

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


Written By
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.

Comments and Discussions

 
GeneralLocks Pin
jpmik13-Apr-09 0:44
jpmik13-Apr-09 0:44 
GeneralRe: Locks Pin
Rahul D.14-Apr-09 1:25
Rahul D.14-Apr-09 1:25 
GeneralMy vote of 1 Pin
SlyLogin1-Apr-09 23:13
SlyLogin1-Apr-09 23:13 
GeneralRe: My vote of 1 Pin
Rahul D.11-Dec-18 22:07
Rahul D.11-Dec-18 22:07 

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.