using System; using System.Collections.Generic; public static class ByteArrayPool { private static Queue pool = new Queue(); public static ArraySegment Take(int count) { // if there are any items to check in the pool if (pool.Count > 0) { for (int i = 0; i < pool.Count; i++) { // get the weakreference to the item var item = pool.Dequeue(); // if the item is alive, someone is using it, // so put it back in the pool and try the next one. if (item.IsAlive) { pool.Enqueue(item); continue; } // if the item has actually been garbage collected, then skip it and // keep searching. var bytes = item.Target as byte[]; if (bytes == null) continue; // if we get to here, the item is good, so keep it in the pool pool.Enqueue(item); // if it has enough capacity, return it. if (bytes.Length >= count) { return new ArraySegment(bytes, 0, count); } } } // nothing was found, so create a new array and track it in the pool. var segment = new ArraySegment(new byte[count]); pool.Enqueue(new WeakReference(segment)); return segment; } }