Discussing the article: "A Generic Object Pool in MQL5: Eliminating Heap Fragmentation in High-Frequency Indicators"

 

Check out the new article: A Generic Object Pool in MQL5: Eliminating Heap Fragmentation in High-Frequency Indicators.

High-frequency MQL5 indicators that instantiate objects on every tick accumulate allocation overhead and timing jitter in OnCalculate(). This article constructs a generic templated object pool using a free-list index array, delivering O(1) Acquire() and Release() operations. The design includes double-release protection, strict separation of payload state from pool metadata in Reset(), and a fixed-capacity free list with no heap fallback. A dual-path custom indicator benchmark measures per-tick overhead difference using GetMicrosecondCount().

Every new operator call in MQL5 requests a contiguous block of memory from the terminal's managed heap. The allocator searches for a free block of sufficient size, marks it as occupied, and returns a pointer. Conversely, the delete operator marks that block as available again. These operations are fast in isolation. However, continuous high-frequency execution introduces overhead that accumulates over time.

The primary cost of repeated new/delete in OnCalculate() is not heap compaction — the specifics of the MQL5 terminal's internal allocator are not publicly documented to that level of detail — but rather the direct overhead of each allocation and deallocation call itself: constructor and destructor invocations, internal allocator bookkeeping, memory initialization, and timing jitter introduced into a hot execution path. Even if individual allocations are fast on average, the unpredictability of their cost matters in a trading context where latency consistency is often more valuable than raw average throughput.

Custom indicators executing OnCalculate() on every tick compound this overhead. During major news events, tick rates can exceed 100 per second. At that speed, the allocator performs two heap operations per tick alongside payload construction and destruction. This workload gradually increases execution time variance across a trading session.

Author: Ushana Kevin Iorkumbul