availableMemoryBytes.php
Table of Contents
Functions
- availableMemoryBytes() : int|null
- Returns the number of bytes this process may reasonably allocate, or `null` when it cannot be told.
Functions
availableMemoryBytes()
Returns the number of bytes this process may reasonably allocate, or `null` when it cannot be told.
availableMemoryBytes([string|null $memoryLimit = null ][, string $meminfoPath = '/proc/meminfo' ]) : int|null
Why this exists. The legacy V1/V2 read path loads the whole file into memory
and peaks at about EncryptionFormat::LEGACY_MEMORY_FACTOR
times its size. Past the budget, PHP either throws a fatal "Allowed memory size
exhausted" or — when memory_limit is -1 — the kernel OOM killer takes the
process down with no exception and no stack trace, so nothing can be reported.
Measured on 2026-10-24 in a 3.8 GB VM: a 1 GB file logged
Out of memory: Killed process (php) in dmesg and nothing anywhere else.
Knowing the budget is what lets OpenSSLFileEncryption::decrypt() refuse
with a message instead of disappearing.
Two sources, in order:
memory_limit— the limit PHP enforces itself, read through parseMemoryLimit. Any positive value wins: it is the ceiling that actually applies, however much RAM the host has./proc/meminfo'sMemAvailable— consulted only whenmemory_limitis-1(unlimited), where the real ceiling is the machine's free memory. Linux only; elsewhere the function returnsnull.
null means unknown, not unlimited: callers should let the operation through
rather than refuse on a number they do not have.
Parameters
- $memoryLimit : string|null = null
-
A
memory_limit-style value to read instead of the ini setting ('512M','-1','1G'…). Testing seam;nullreadsini_get('memory_limit'). - $meminfoPath : string = '/proc/meminfo'
-
Path to read
MemAvailablefrom. Testing seam.
Tags
Return values
int|null —The budget in bytes, or null when no source gives a figure.