php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70341 Weird behaviour of memory_get and peak_usage
Submitted: 2015-08-24 12:42 UTC Modified: 2015-08-27 14:49 UTC
From: quantuumsnot at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: *General Issues
PHP Version: 7.0.0RC1 OS: windows 7 x64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: quantuumsnot at gmail dot com
New email:
PHP Version: OS:

 

 [2015-08-24 12:42 UTC] quantuumsnot at gmail dot com
Description:
------------
I usually use this short script to test some home-brew functions but noticed a inconsistency in memory_get_usage() and memory_get_peak_usage()

Corresponding to docs, when using:
FALSE parameter - emalloc() returns bytes used by the script
TRUE parameter - returns bytes allocated by OS

The problem is that the script worked on PHP 5.6.8 x64 Thread Safe - returned bytes are above 0, but in 7.0.0RC1 this returns 0
The fix is to use at line #1 memory_get_usage(false) which returns 40-50 times!!! bigger memory usage (for example 1.6-1.8MB instead of 40-45KB)
Tested it on 3v4l.org and the results are the same, 5.5.0-5.6.12 always win

What is the correct way to benchmark my code in PHP7 using pure PHP? (do not suggest xdebug or frameworks, please)

Test script:
---------------
$memoryUsage = memory_get_peak_usage(false);
someFunctionCalled();
$memoryUsage = abs(memory_get_peak_usage(false) - $memoryUsage);


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-24 12:45 UTC] quantuumsnot at gmail dot com
To be precise - using false gives me 0 bytes, using true gives me a rly big difference in bytes used
 [2015-08-24 20:00 UTC] cmb@php.net
-Status: Open +Status: Feedback
 [2015-08-24 20:00 UTC] cmb@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with ,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

See <https://3v4l.org/0SjGE> for expected behavior.
 [2015-08-26 20:34 UTC] quantuumsnot at gmail dot com
This problem is valid for devster's 'Ubench' script too:
---
<?php

error_reporting(-1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
gc_enable();

// https://github.com/devster/ubench
require_once 'src/Ubench.php';

$bench = new Ubench;
$testSum = 0;
$bench->start();
// Execute some code
for ($i = 0; $i < 1000000; $i++) { $j= $i; }
$bench->end();

// Get elapsed time and memory
echo "<br />" . $bench->getTime();
echo "<br />" . $bench->getTime(true); // elapsed microtime in float
echo "<br />" . $bench->getTime(false, '%d%s');

echo "<br />" . $bench->getMemoryPeak();
echo "<br />" . $bench->getMemoryPeak(true); // memory peak in bytes
echo "<br />" . $bench->getMemoryPeak(false, '%.3f%s');

// Returns the memory usage at the end mark
echo "<br />" . $bench->getMemoryUsage();

exit();

?>
 [2015-08-26 20:50 UTC] cmb@php.net
> Tested it on 3v4l.org and the results are the same, 5.5.0-5.6.12 always win

Can you please post a link to the 3v4l.org test?
 [2015-08-27 01:44 UTC] quantuumsnot at gmail dot com
https://3v4l.org/COXWa
 [2015-08-27 10:17 UTC] cmb@php.net
You have claimed

> The fix is to use at line #1 memory_get_usage(false) which
> returns 40-50 times!!! bigger memory usage (for example
> 1.6-1.8MB instead of 40-45KB)

and later corrected

> using false gives me 0 bytes, using true gives me a rly big
> difference in bytes used

However, <https://3v4l.org/COXWa> doesn't show this behavior.
 [2015-08-27 14:40 UTC] quantuumsnot at gmail dot com
UBench returns this on my setup (nothing changed):
2.00Mb
2097152
2.000Mb
2.00Mb
 [2015-08-27 14:49 UTC] cmb@php.net
-Status: Feedback +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-08-27 14:49 UTC] cmb@php.net
Yes, that is exactly what is reported on <https://3v4l.org/COXWa>
for PHP 7. However,

  2097152 / 1024 / 1024 = 2
 [2015-08-27 19:16 UTC] quantuumsnot at gmail dot com
Should I show my script for monitoring how much memory each function uses? Results are stored in SQLite database

http://wikisend.com/download/556368/metrics.db
[URL=http://wikisend.com/download/556368/metrics.db]metrics.db[/URL]
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 21:01:32 2024 UTC