tags:

views:

65

answers:

4

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:

  1. which structure is good for this purpose? byte[10MB], List<bytes>(10MB), List<MyStruct>(24000), or ...?
  2. how to quickly wipe out the data (not List.Clear(), just set the value to 0) for List or array?
  3. If I say 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()?
  4. List.Insert() will make the List larger or it just replace the existing item?
A: 
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.
Jimmy Hoffa
There is a certain logic to the rule: If it implements IDisposable, Dispose of it. But sometimes we are allowed to exercise our brains. The purpose of IDisposable is to relinquish *external* resources long before the GC gets around to releasing the memory. What external resources do you imagine that a MemoryStream has? I'll give you the answer: None.
Tergiver
A: 

If you do use a byte[] you can call Initialize() to set every member to its default value (0).

Jon B
+1  A: 

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

  1. byte[]
  2. no need to clear the array, just keep track of where the 'end' of your current cycle is
  3. n/a
  4. n/a
BioBuckyBall
A: 

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.

Dean J
size is not always the same
5YrsLaterDBA
If it's always in the same ballpark, this still works pretty well; if you're always looking at 5-10 megs, and never more than 10 megs, give yourself a 10 meg array buffer.
Dean J