rex
rex

Reputation: 3183

Reasonable size of List<ReferenceType> to have in memory

I know that there are limitations on the size of a List you can have in C# (as discussed here).

Assuming that my hardware permits it, would it be bad if I just let my List grow in-memory and use it from there, obviously as long as it doesn't exceed its capacity? I feel a bit naughty doing this. I expect my machine to be a couple of gigs in RAM.

If not, what would be a reasonable size to limit my List and then subsequently push to a database? My use-case will include asynchronously (and thread-safely) appending reference types to the list, as well as querying it (using Linq) for a set of the data based on their properties (eg time-based). The list will essentially be a time-series (ie time-stamped objects of data). I want to be able to do streaming analytics on the data (eg moving average), as well as time-based lookups, that may return the entire list.

Let's assume my list will be holding, at its largest, class objects (ie reference types), of around 3-5 million.

UPDATE:

As a reference, I have previously written the objects that are being appended to the list as JSONs to a text file (1 JSON per line). The size of the .txt file for 8,658 items is only 1,570KB. Extrapolating that to 5m objects comes close to 1GB.

Upvotes: 1

Views: 264

Answers (1)

nan
nan

Reputation: 20296

There are no limitations of size of List in .NET (besides the hard limits described in the link you provided). Apart from that the list will grow in memory until memory runs out and OutOfMemoryException is thrown. This is more probable than reaching hard coded limits.

Not removing objects from a collection is one of the causes of memory leak in C# code. Especially when the collection is static which ensures it will live until AppDomain is unloaded.

I would advise you to keep track of objects and remove them as soon as you don't need them.

Upvotes: 2

Related Questions