Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9312579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:39:49+00:00 2026-06-19T01:39:49+00:00

List is a wrapper over array. While you add items to the list it

  • 0

List is a wrapper over array. While you add items to the list it creates bigger and bigger array undercover (and previous array is garbage collected). But if you treat large lists at some point you will get OutOfMemoryException even if there is free memory due to memory fragmentation. I am looking for an ICollection implementation which would work with set of arrays undercover similar to what MemoryTributary does.

Update.

I have found BigArray implementation here:

http://blogs.msdn.com/b/joshwil/archive/2005/08/10/450202.aspx.

While it tries to solve other problem (creating an array of >2GB size), it solves my problem too. But this implementation is not full and even does not compile. So if I don’t find any better, I will improve this one and use it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:39:50+00:00Added an answer on June 19, 2026 at 1:39 am

    I haven’t found any good implementation. So I have written my own ChunkyList. Generally ChunkyList is a List of arrays wrapper. Initially block has size of 1 but it is multiplied by two (when it reaches MaxBlockSize, a next block is created) every time you need to extend the current block (the behavior similar to the List).

    Here is a generic ChunkyList:

    public class ChunkyList<T> : IList<T>
    {
        public ChunkyList()
        {
            MaxBlockSize = 65536;
        }
    
        public ChunkyList(int maxBlockSize)
        {
            MaxBlockSize = maxBlockSize;
        }
    
        private List<T[]> _blocks = new List<T[]>();
    
        public int Count { get; private set; }
    
        public int MaxBlockSize { get; private set; }
    
        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }
    
        public IEnumerator<T> GetEnumerator()
        {
            var index = 0;
            foreach (var items in _blocks)
                foreach (var item in items)
                {
                    yield return item;
                    if (Count <= ++index)
                        break;
                }
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    
        public void Add(T item)
        {
            var indexInsideBlock = GetIndexInsideBlock(Count);
            if (indexInsideBlock == 0)
                _blocks.Add(new T[1]);
            else
            {
                var lastBlockIndex = _blocks.Count - 1;
                var lastBlock = _blocks[lastBlockIndex];
                if(indexInsideBlock >= lastBlock.Length)
                {
                    var newBlockSize = lastBlock.Length*2;
                    if (newBlockSize >= MaxBlockSize)
                        newBlockSize = MaxBlockSize;
    
                    _blocks[lastBlockIndex] = new T[newBlockSize];
                    Array.Copy(lastBlock, _blocks[lastBlockIndex], lastBlock.Length);
                }
            }
    
            _blocks[GetBlockIndex(Count)][indexInsideBlock] = item;
    
            Count++;
        }
    
        public void AddRange(IEnumerable<T> items)
        {
            foreach (var item in items)
                Add(item);
        }
    
        public void Clear()
        {
            throw new NotImplementedException();
        }
    
        public bool Contains(T item)
        {
            throw new NotImplementedException();
        }
    
        public void CopyTo(T[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }
    
        public bool Remove(T item)
        {
            throw new NotImplementedException();
        }
    
        public int IndexOf(T item)
        {
            throw new NotImplementedException();
        }
    
        public void Insert(int index, T item)
        {
            throw new NotImplementedException();
        }
    
        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }
    
        public T this[int index]
        {
            get
            {
                if (index >= Count)
                    throw new ArgumentOutOfRangeException("index");
    
                var blockIndex = GetBlockIndex(index);
                var block = _blocks[blockIndex];
    
                return block[GetIndexInsideBlock(index)];
            }
            set { throw new NotImplementedException(); }
        }
    
        private int GetBlockIndex(int index)
        {
            return index / MaxBlockSize;
        }
    
        private long GetIndexInsideBlock(int index)
        {
            return index % MaxBlockSize;
        }
    }
    

    And here are tests which prove this implementation works:

     [TestClass]
        public class ChunkyListTests
        {
            [TestMethod]
             public void GetEnumerator_NoItems()
             {
                 var chunkyList = new ChunkyList<float>();
    
                 var wasInsideForeach = false;
                 foreach (var item in chunkyList)
                     wasInsideForeach = true;
    
                 Assert.IsFalse(wasInsideForeach);
             }
    
            [TestMethod]
            public void GetEnumerator_MaxBlockSizeOfThreeWithThreeItems()
            {
                var chunkyList = new ChunkyList<float> (3) { 1, 2, 3 };
    
                var wasInsideForeach = false;
                var iteratedItems = new List<float>();
                foreach (var item in chunkyList)
                {
                    wasInsideForeach = true;
                    iteratedItems.Add(item);
                }
    
                Assert.IsTrue(wasInsideForeach);
                CollectionAssert.AreEqual(new List<float> { 1, 2, 3 }, iteratedItems);
            }
    
            [TestMethod]
            public void GetEnumerator_MaxBlockSizeOfTwoWithThreeItems()
            {
                var chunkyList = new ChunkyList<float>(2) {1,2,3};
                var wasInsideForeach = false;
                var iteratedItems = new List<float>();
    
                foreach (var item in chunkyList)
                {
                    wasInsideForeach = true;
                    iteratedItems.Add(item);
                }
    
                Assert.IsTrue(wasInsideForeach);
                CollectionAssert.AreEqual(new List<float>() { 1, 2, 3 }, iteratedItems);
                Assert.AreEqual(chunkyList.MaxBlockSize, 2);
            }
        }
    

    P. S. I have implemented only those IList methods which are used in my code. So you are welcome to improve this implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class, which is just a wrapper over a list, i.e., public
List<SelectListItem> items = new List<SelectListItem>(); if (a) { SelectListItem deliveryItem = new SelectListItem() {
List<Foo> fooList = Session[foo] as List<Foo>; fooList.Add(bar); Does the call to Add() change the
List<Car> oUpdateCar = new List<Car>(); oUpdateCar.Add(new Car()); oUpdateCar[0].name = Color; oUpdateCar[0].value = red; oUpdateCar.Add(new
I'm having a problem in IE9: Whenever i hover over my top level wrapper
I'm new to MVC, but I've been all over this, read all the documentation
<div class=share-tooltip-wrapper data-id=<?php echo $post_ID; ?>> <div class=share-tooltip> <div class=arrow></div> <ul class=share-list> <li> <a
I have a simple UL list, when you hover over the More list item
I have a list of items in my web application; each of these has
I have used Spring DATA JPA in my application, which is wrapper over hibernate.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.