Reputation: 34840
Plan to have a data structure to store temporary binary data in the memory for analysis.
The max size of the data will be about 10MB
.
data will be added at the end 408 bytes
at a time.
no search, retrieve operations on those temporary binary data.
data will be wipe out and the storage will be reused for next analysis.
questions:
byte[10MB], List<bytes>(10MB), List<MyStruct>(24000), or ...?
List.Clear(),
the memory for this List
will shrink or the capacity (memory) of the List is still there and no memory allocation when I call List.AddRange()
after the Clear()?
Upvotes: 4
Views: 571
Reputation: 40369
If your data is usually the same size, and always under a certain size, use a byte array.
Create a byte[], and a int that lets you know where the end of the "full" part of that buffer stops and the "free" part starts. You never need to clear it; just overwrite what was there. The only problem with this is if your data is sometimes 100 kb, sometimes 10 MB, and sometimes a bit larger than you originally planned for.
List will be slower to use and larger in memory, although they handle various sizes of data out of the box.
Upvotes: 0
Reputation: 10758
You will have to describe what you are doing more to give better answers but it sounds like you are worried about efficiency/perf so
Upvotes: 1
Reputation: 5967
using (System.IO.MemoryStream memStream = new System.IO.MemoryStream())
{
Do stuff
} // the using ensures proper simple disposal occurs here so you don't have to worry about cleaning up.
Upvotes: 0