php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70459 memory_get_usage(true) returns wrong result
Submitted: 2015-09-08 10:23 UTC Modified: 2021-09-19 04:22 UTC
From: a dot schilder at gmx dot de Assigned: cmb (profile)
Status: No Feedback Package: PHP options/info functions
PHP Version: 7.0.0RC2 OS: Windows 7 Pro
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-09-08 10:23 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-08 08:36 UTC] 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
 [2018-02-28 21:57 UTC] cmb@php.net
-Package: Unknown/Other Function +Package: PHP options/info functions
 [2021-09-08 16:15 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2021-09-08 16:15 UTC] cmb@php.net
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?
 [2021-09-19 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Oct 23 09:03:33 2021 UTC