To reduce memory garbage production the plugin reuses as much memory as it can. To support this, the plugin implements and uses a byte array pooling mechanism through the
BufferPool static class. The plugin uses this class to allocate and release
bytes back to the pool.
To avoid keeping reference to too much memory the plugin requests buffers with a minimum size. This way if the
BufferPool has no buffer for the requested size, it can return a larger buffer. Also, buffers stored in the pool can time out, releasing back to the runtime for garbage collection.
Pooling mechanism can be disabled, configured and used by outside of the plugin. Configurable fields are the following:
Setting this field to false the pooling mechanism can be disabled. Its default value is
Buffer entries that released back to the pool and older than this value are removed from the pool (so the GC can collect them) when next maintenance is triggered. Its default value is 30 seconds.
How often pool maintenance must run. Its default value is 10 seconds.
Minimum buffer size that the plugin will allocate when the requested size is smaller than this value, and canBeLarger is set to true. Its default value 256 bytes.
Maximum size of a buffer that the plugin will store. Its default value is
Maximum accumulated size of the stored buffers. Its default value is 10 Mb.
Whether to remove empty buffer stores from the free list. Its default value is
If it set to true and a byte is released more than once it will log out an error. Its default value is
true when run in the editor,
So to disable pooling the following line can be added:
BestHTTP.PlatformSupport.Memory.BufferPool.IsEnabled = false;
The following functions are available to use the pool:
byte Get(long size, bool canBeLarger)
Get a byte array from the pool. If
true, the returned buffer might be larger than the requested size.
void Release(byte buffer)
Release back a byte array to the pool.
byte Resize(ref byte buffer, int newSize, bool canBeLarger)
Resize a byte array. It will release the old one to the pool and get a new one from the pool.
Remove all stored entries instantly.
BufferSegment is a wrapper around a byte and you should not use the Data’s Length property! BufferSegment is used when the byte is from the BufferPool, its length is usually is power of two. The array usually larger than the real payload it contains (so the BufferSegment’s Count is not equal to its Data’s Length) and the payload might not start on the Data’s zeroth index (hence the BufferSegment’s Offset property).