Adds the specified value to the concurrent bag, returning a WeakReference
that wraps the value.
Performs garbage collection, removing any empty weak references.
Returns a weakly consistent iterator over the bag.
Returns a weakly consistent iterator over the bag. This iterator will never throw exceptions even in the presence of concurrent modifications.
Returns the size of the bag.
Returns the size of the bag. Due to concurrent modification, this is only an estimate. Note this operation is O(n.max(m)), where n is the number of elements in the collection, and m is the table size.
A WeakConcurrentBag stores a collection of values, each wrapped in a
WeakReference
. The structure is optimized for addition, and will achieve zero allocations in the happy path (aside from the allocation of theWeakReference
, which is unavoidable). To remove a value from the bag, it is sufficient to clear the corresponding weak reference, at which point the weak reference will be removed from the bag during the next garbage collection.Garbage collection happens regularly during the
add
operation. Assuming uniform distribution of hash codes of values added to the bag, the chance of garbage collection occurring during anadd
operation is 1/n, wheren
is the capacity of the table backing the bag.