Oihana PHP

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:

  1. 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.
  2. /proc/meminfo's MemAvailable — consulted only when memory_limit is -1 (unlimited), where the real ceiling is the machine's free memory. Linux only; elsewhere the function returns null.

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; null reads ini_get('memory_limit').

$meminfoPath : string = '/proc/meminfo'

Path to read MemAvailable from. Testing seam.

Tags
example
use function oihana\files\openssl\availableMemoryBytes;

availableMemoryBytes() ;              // 268435456 with memory_limit = 256M
availableMemoryBytes( '-1' ) ;        // MemAvailable on Linux, null on macOS
author

Marc Alcaraz (ekameleon)

since
1.5.0
Return values
int|null —

The budget in bytes, or null when no source gives a figure.

On this page

Search results