<p>I'm creating an app that holds loads of loads of user data in memory, and it's mostly keeping it all in List&lt;T&gt; structures (and some Dictionary&lt;T,T&gt; when I need lookup).</p>

<p>And I'm wondering...</p>

<p>How efficient are Lists?
How much memory overhead do I get for each of them? (that is, memory space in addition to what the objects they contain would take)
How much of a penalty do I pay every time I instance a new one?</p>

<p>Is there a more efficient way?</p>

<p>Dictionaries are just HashTables, right? Or are them a less efficient data structure?</p>

<p>I'd like to use Arrays, but I have the typical problem of adding and removing things all the time from them, so having to grow / shrink them would be a pain.</p>

<p>Any ideas/suggestions?</p>

<hr>

<p>Edit: I know my basic data structures 101, and why a Linked List is better for adding/removing, and a HashTable is better for Random Access.</p>

<p>I'm mostly concerned about .Net's idionsyncracies. How much memory each of these structure wastes, for example. And time wasted on initializing / killing them.</p>

<p>Things like, for example, if it takes a lot of time to instance/GC a List, but not much to clear it, maybe I should keep a little pool of Lists waiting for me, and clear them and send them back to the pool when done, instead of simply dereferencing them.</p>

<p>Or, if Hashtables are faster for access but waste a lot of memory, I might prefer to use Lists and traverse them, for small item counts.</p>

<p>And I'd also really like to focus on memory usage, since my app is hediously memory intensive (think memcached like)...
Does anyone know where I can find such info?</p>

[{'answer_id': 33407, 'author': 'Nick', 'author_id': 1490, 'author_profile': 'https://Stackoverflow.com/users/1490', 'pm_score': 2, 'selected': False, 'text': '<p>Lists are arrays underneath, so the performance hit of adding an item, unless it is at the end, will be very costly.</p>\n\n<p>Otherwise they will be basically as fast as an array.</p>\n'}, {'answer_id': 33424, 'author': 'Nir', 'author_id': 3509, 'author_profile': 'https://Stackoverflow.com/users/3509', 'pm_score': 2, 'selected': False, 'text': "<p>List uses an array internally and Dictionary uses an hash table.</p>\n\n<p>They are faster then the older non-generic classes ArrayList and HashTable because you don't have the cost of converting everything to/from object (boxing, unboxing and type checking) and because MS optimized them better then the old classes.</p>\n"}, {'answer_id': 33427, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 2, 'selected': False, 'text': '<p>Maybe you should consider using some type of in-memory database if you have that much data that has to be held in the memory,</p>\n'}, {'answer_id': 33432, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 2, 'selected': False, 'text': '<p>If you need efficiency in inserting or removing at random places in the list there is a LinkedList data structure - the <a href="http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx" rel="nofollow noreferrer">MSDN Article</a> gives details. Obviously being a linked list random access isn\'t efficient.</p>\n'}, {'answer_id': 33450, 'author': 'Jesse Dearing', 'author_id': 1804, 'author_profile': 'https://Stackoverflow.com/users/1804', 'pm_score': 2, 'selected': False, 'text': '<p>The LinkedList object would take less time to add to and remove from because of the nature of linked lists.  When you add an element it does not have to resize an array like a normal list does.  Other than that improvement I would suspect that the LinkedList would perform about the same as a normal List.</p>\n\n<p>See this on Wikipedia: <a href="http://en.wikipedia.org/wiki/Linked_list#Linked_lists_vs._arrays" rel="nofollow noreferrer">Linked Lists vs. Arrays</a></p>\n'}, {'answer_id': 33464, 'author': 'Nick', 'author_id': 1490, 'author_profile': 'https://Stackoverflow.com/users/1490', 'pm_score': 1, 'selected': False, 'text': "<p>If you are concerned about memory usage, the real key is to store your array on disk and map just the parts you need into memory at that time.</p>\n\n<p>The key is to use FILE_FLAG_NO_BUFFERING and always read/write exactly one sector's worth of data.</p>\n"}, {'answer_id': 33473, 'author': 'Dan Blair', 'author_id': 1327, 'author_profile': 'https://Stackoverflow.com/users/1327', 'pm_score': 0, 'selected': False, 'text': "<p>The .Net List doesn't use a linked list.  It is an array, it starts with 4 positions by default and I think it doubles in size as you add things.  So performance can vary a bit depending on how you use it.</p>\n\n<hr>\n\n<p>If your using VS 2008 run the profiler before you get too far down this rat hole.  When we started actually looking at where we were losing time it didn't take long for use to figure out that debating the finer points of linked lists just really didn't matter.  </p>\n"}, {'answer_id': 33539, 'author': 'Nick', 'author_id': 1490, 'author_profile': 'https://Stackoverflow.com/users/1490', 'pm_score': 1, 'selected': False, 'text': '<p>I think the two-process thing might be overkill; plus the interprocess communication will likely have some slowness (although I\'ve never tried such a thing so take my opinion of it as a grain of salt).  I work on a data-driven application where each data unit is tiny, but we may have upwards of a billion data units at any given time.  The method we use is basically:</p>\n\n<ul>\n<li>Everything resides on disk, no matter what</li>\n<li>Data is blocked into "chunks"; each chunk knows when it was last accessed</li>\n<li>Chunks are dragged up from disk into memory when they are needed</li>\n<li>A low-priority thread monitors memory usage and deletes the least recently used stuff</li>\n</ul>\n\n<p>In other words, it\'s a homebrew caching scheme.  The benefit is you can control with pinpoint accuracy what data is in memory, which you cannot if you rely on the OS paging scheme.  If some commonly used variable ends up mixed in with your data on a page, that page will be repeatedly hit and prevent it from going to disk.  If you design into your application an accommodation that some data requests will take longer than others, then this will work pretty well.  Particularly if you know what chunks you will need ahead of time (we don\'t).</p>\n\n<p>Keep in mind that everything in a .NET app has to fit within 2 GB of memory, and because of the way the GC works and the overhead of you app, you actually probably have somewhat less than that to work with.</p>\n\n<p>To spy on exactly what your heap looks like and who is allocating, use the <strong>CLR profiler</strong>: <a href="http://www.microsoft.com/downloads/details.aspx?familyid=86ce6052-d7f4-4aeb-9b7a-94635beebdda&amp;displaylang=en" rel="nofollow noreferrer">http://www.microsoft.com/downloads/details.aspx?familyid=86ce6052-d7f4-4aeb-9b7a-94635beebdda&amp;displaylang=en</a></p>\n'}, {'answer_id': 36587, 'author': 'McKenzieG1', 'author_id': 3776, 'author_profile': 'https://Stackoverflow.com/users/3776', 'pm_score': 2, 'selected': False, 'text': '<p>If you really want to see all the gory details of how List&lt;> and Dictionary&lt;,> are implemented, use the wonderfully useful <a href="http://www.red-gate.com/products/reflector/" rel="nofollow noreferrer">.NET Reflector</a>.</p>\n\n<p>See also the documentation for the excellent <a href="http://www.itu.dk/research/c5/" rel="nofollow noreferrer">C5 Generic Collection Library</a>, which has very good implementations of a number of collection types missing from the BCL.</p>\n'}, {'answer_id': 943253, 'author': 'pupeno', 'author_id': 6068, 'author_profile': 'https://Stackoverflow.com/users/6068', 'pm_score': 0, 'selected': False, 'text': "<p>I wouldn't move a finger until there's some performance problem and a profiler showed you were it is. Then you'll have a definitive problem to solve and it'll be much easier.</p>\n"}]
['https://Stackoverflow.com/questions/33403', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3314/']