Memory consumption of generic lists

  • Thread starter Thread starter Hannes
  • Start date Start date
H

Hannes

Hi guys!

I use a generic list which, once filled, will not change. It contains a
few 100'000 entries, so I'd like to limit its memory consumption. If I
set its capacity to its count after filling it, does the extra unused
memory (due to its probably high capacity by this time) get freed up do
I have to copy it into a new list where I set the capacity before
filling?
It is not possible to know how many entries it will contain before
filling starts.

Thanks in advance for any answers!
BTW: Is there a good online resource for C# out there? VisualStudio's
helpfile is really lousy...

Cheerio,
Hannes
 
Well, depending on what functionality of the list you need, you could always
use a List<T> to *fill* the data, then call .ToArray() and just keep the
array. This makes it very explicit that it is intended to be fixed size.

When 3.0 comes out, I *believe* that LINQ will ensure that even arrays
(since they support IEnumerable) have a greater range of query methods. We
shall see ;-p

Re the C# resource... google is your friend; it depends what level you
want... "Ecma-334" is a very good reference, for instance...

Marc
 
call .ToArray()

Ooh, sweet! Exactly what I wanted. Thank you!
Re the C# resource... "Ecma-334"

I'll give that a shot, thanks.

Cheerio,
Hannes
 
Hannes,

You can call the TrimExcess method to compact the list. 100,000 is a
lot of items to store in memory. I don't know how big each item is,
but if they're really big you might want to consider using a
compression strategy.

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top