Memory Usage Reporter
The platform exposes two different sets of memory counters, and they answer two different questions on both MQL4 and MQL5. TERMINAL_MEMORY_* (read via TerminalInfoInteger) describes the whole terminal application - is this machine under memory pressure at all, useful context on a VPS running several terminals side by side. MQL_MEMORY_* (read via MQLInfoInteger) describes only the currently running program's own allocations. The distinction matters because a bug that slowly leaks memory inside one EA - the classic case is an array that gets ArrayResize()'d larger every tick and never shrinks - barely moves the whole-terminal number for a long time. The terminal has gigabytes; one array creeping into the tens of megabytes is noise against that total. But it dominates the program's OWN number almost immediately, because that figure has nothing else in it. Watching MemoryProgramUsedKB() over a session catches the leak while it is still small; waiting for the terminal-wide figure to move means waiting until it is already a problem. This header just wraps both counter families in readable units (MB for the terminal figures, which the platform already reports in MB; KB for the program figures, which the platform reports in bytes) and adds a usage percentage with an explicit sentinel: -1.0 when the terminal did not report a usable total, so a caller cannot mistake "no data" for "0% used" if it naively checks percent < threshold without first checking for the sentinel. Notes and limitations - All values come from the platform's own counters; this header adds no measurement of its own, only unit conversion and the usage-percent sentinel. Any imprecision in the underlying TERMINAL_MEMORY_* / MQL_MEMORY_* values is inherited as-is, on both platforms. - MemoryProgramUsedKB() reflects the calling program's own MQL-level allocations. It does not account for memory the terminal itself holds on the program's behalf outside that accounting (chart objects, indicator buffers created via iCustom/indicator handles on other programs, and so on). - Treat MemoryUsagePercent() returning -1.0 as "unknown", never as "0% used" - a caller that skips the sentinel check and alerts only above a high threshold will silently never alert on a terminal that stops reporting a total at all. - This measures memory only. For where the CPU time is going, see Profiler.mqh, a separate submission.
Script · MT4 · v1.0 · Free
About Memory Usage Reporter
Memory Usage Reporter is a script for MetaTrader 4. It has 1 setting, such as Array elements. MQL source code included.
Files in the download: MemoryReport.mqh, MemoryReport.txt, MemoryReportDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Array elements | 500000 | Size of the array grown to demonstrate rising own-memory use |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code