Bug #70459 [Opn->Fbk]: memory_get_usage(true) returns wrong result

From: Date: Wed, 08 Sep 2021 16:15:17 +0000
Subject: Bug #70459 [Opn->Fbk]: memory_get_usage(true) returns wrong result
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-236472@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70459&edit=1 ID: 70459 Updated by: cmb@php.net Reported by: a dot schilder at gmx dot de Summary: memory_get_usage(true) returns wrong result -Status: Open +Status: Feedback Type: Bug Package: PHP options/info functions Operating System: Windows 7 Pro PHP Version: 7.0.0RC2 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: This doesn't look like a bug to me. The Zend memory manager is optimized for typical Web scenarios, and tries to automatically find a good ratio between allocating and releasing memory to the OS. Often, it is preferable to overallocate, even if the memory is not immediately used. If it isn't used for several requests, it might be good to release the memory. Did you compare the returned values with what other system tools report? Previous Comments: ------------------------------------------------------------------------ [2016-06-08 08:36:39] sailormax at inbox dot lv Possible reason of my "Out of memory" problem. memory_limit = 2280M but after some time of using my local site: Fatal error: Out of memory (allocated 2097152) (tried to allocate 4096 bytes) in ... Helps only restart of Apache. Windows 10 v1511 10586.318 64 bit Apache/2.4.18 (Win32) OpenSSL/1.0.2h PHP/7.0.7 OpCache: used_memory=17.89 MB, free_memory=227.75 MB, wasted_memory=10.36 MB Apcu: 5.1.3 Free=30.8 MBytes, Used=1.2 MBytes ------------------------------------------------------------------------ [2015-09-08 10:23:32] a dot schilder at gmx dot de Description: ------------ I did some memory tests to compare PHP 7.0.0RC2 (TS, 64bit) with 5.x versions and realized a strange behavior when using memory_get_usage() with $real_usage set to TRUE. I did a first call of memory_get_usage(true), produced some data and did a second call of memory_get_usage(true), before using the data. Then I compared the logged memory consumption. In the example below I got a memory difference of about 78 MB when calling the script the first time, the next time I called the script I got about the half of this value, and further calls reduced it more and more until I got to 0 bytes between the first and second call of memory_get_usage(true). After a restart of the web server (Apache 2.4) I get the same behavior again. NOTE: $real_usage set to FALSE works as expected. Test script: --------------- <?php $oldMem = memory_get_usage(true); $array = array(); for ($i=1; $i <= 10000; $i++) { $array[$i] = range(1, 96); } $newMem = memory_get_usage(true); echo "Array size: " . count($array) . "<br>"; echo "Memory consumption BEFORE: " . $oldMem . "<br>"; echo "Memory consumption AFTER: " . $newMem . "<br>"; Expected result: ---------------- Result of the first 10 calls after the restart of the web server: Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Actual result: -------------- Result of the first 10 calls after the restart of the web server: Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 83886080 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 44040192 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 23068672 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 12582912 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 8388608 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 6291456 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 4194304 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 4194304 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 4194304 Array size: 10000 Memory consumption BEFORE: 2097152 Memory consumption AFTER: 2097152 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70459&edit=1

« previous php.bugs (#236472) next »