views:

19

answers:

1

Whats the pro's and con's of creating a collection instance on the fly or beforehand, during initialisation for use later.

I have a whole bundle of threads that each need to output a buffer which is enqueued on a priority or intervalheap queue. I was wondering if it would be more effecient in c# to create, a circular buffer of type X, of size 2048 beforehand, and just write into each one, and reuse later, or allow each thread to create a buffer on the fly and enqueue it, i.e. create them when necessary, and allow for normal cleanup as per.

I know that the GC would try and disappear the pre-created circular queue. I've had strange debugging problems in the past, looking for object that no longer the exist, because the GC removed it as per.

Any help or advice would be appreciated.

Bob.

+1  A: 

GC won't remove an object you still have a reference to - in other words, if you were able to use your pre-created buffer, it wouldn't be garbage collected - unless you had a WeakReference to it, of course.

Do you know that this will be a performance bottleneck at all? Why not write the simplest code that works first, and measure how well it performs?

Jon Skeet
First point, yip I know. Second point, I don't know if it will be a bottleneck, certainly creating it on the fly would be less code.
scope_creep