Reputation: 5771
I am trying to calculate the maximum safe size for a list before needing to create a new one. I am thinking about using the size of the data structure, based off of the type, and dividing 1 GB (since 2 GB is the < .Net 4.5 limit) by it to see how many elements I can possibly stick in a list safely. Perhaps maxnumber - 3, in case of overhead and what not.
Your thoughts?
Upvotes: 1
Views: 236
Reputation: 100630
You should be able to get list of items that is backed by byte array of almost 2Gb (if you try to grow the list dynamically it will obviously fail close to half of max size due to reallocation stategy).
If you data is not immutable you may instead consider GC friendlier approach that does not create many objects on LOH. Try targeting all allocations to be under limit of LOH allocation (I think 85Kb) and measure if code behaves better.
Upvotes: 1
Reputation: 39079
If your list contains structures, your approach would work, but you'll need to take memory alignment into account (it can cause an error of much more than 3...). If it's a reference type, your element size is 4 (on a 32-bit CLR) or 8 (on a 64-bit CLR).
However, if this is even a problem for you, you should consider using an alternative. Either store the entire list elsewhere (a database?) and bring the parts you need to memory, or use a different data structure, such as a list of lists (basically fragmenting your data), placing 100,000 elements in each small list.
Upvotes: 1