diff --git a/ext/standard/VmMemory.php b/ext/standard/VmMemory.php index c07592fc631..cef54d377d4 100644 --- a/ext/standard/VmMemory.php +++ b/ext/standard/VmMemory.php @@ -69,27 +69,24 @@ private static function readEmallocApprox(): int return $rss; } + /** + * RSS via /proc/self/statm only (issue #7287, #4862). + * + * JIT/AOT use the same source in MemoryRuntime::__phpc_memory_read_rss_bytes. + */ private static function readRssBytes(): int { - if ('Linux' === \PHP_OS_FAMILY && is_readable('/proc/self/statm')) { - $statm = @file_get_contents('/proc/self/statm'); - if (false !== $statm && '' !== $statm) { - $parts = preg_split('/\s+/', trim($statm)); - $rssPages = (int) ($parts[1] ?? 0); - - return $rssPages * self::pageSize(); - } + if ('Linux' !== \PHP_OS_FAMILY || !is_readable('/proc/self/statm')) { + return 0; } - - if (\function_exists('getrusage')) { - $ru = getrusage(); - if (\is_array($ru) && isset($ru['ru_maxrss'])) { - // Linux: kilobytes; BSD often bytes — treat as KiB (php-src ZEND_SYS_VMEM). - return (int) $ru['ru_maxrss'] * 1024; - } + $statm = @file_get_contents('/proc/self/statm'); + if (false === $statm || '' === $statm) { + return 0; } + $parts = preg_split('/\s+/', trim($statm)); + $rssPages = (int) ($parts[1] ?? 0); - return 0; + return $rssPages * self::pageSize(); } private static function pageSize(): int diff --git a/test/unit/VmMemoryTest.php b/test/unit/VmMemoryTest.php new file mode 100644 index 00000000000..5d5b4f7d8db --- /dev/null +++ b/test/unit/VmMemoryTest.php @@ -0,0 +1,29 @@ +assertDoesNotMatchRegularExpression('/function_exists\\s*\\(\\s*[\'"]getrusage/', $source); + $this->assertDoesNotMatchRegularExpression('/\\\\getrusage\\s*\\(/', $source); + $this->assertStringContainsString('/proc/self/statm', $source); + } + + public function testRealUsageReadsProcStatmOnLinux(): void + { + if ('Linux' !== \PHP_OS_FAMILY || !is_readable('/proc/self/statm')) { + $this->markTestSkipped('Linux /proc/self/statm required for RSS probe'); + } + + $this->assertGreaterThan(0, VmMemory::getUsage(true)); + } +}