How do I know how much memory my application uses in PHP?

Asked

Viewed 1,740 times

5

I wonder if there is any way to know how much my PHP application is consuming memory.

For example: I want to know, at the end of the script execution, how much PHP spent on the execution process of a given script.

Is there any PHP function I can do this check for?

3 answers

5


As has already been quoted, memory_get_usage captures current memory expenditure. However, there is another function, the memory_get_peak_usage, that picks up the maximum recorded memory peak.

Note: so much memory_get_usage how much memory_get_peak_usage only achieves the result defined by emalloc() is returned.

So the differences are:

  • memory_get_usage returns the approximate current memory expense, for example:

    <?php
    
    echo round(memory_get_usage() / 1024, 4), 'Kb', PHP_EOL; // 219.2656Kb
    
    $a = str_repeat("Hello", 4242);
    
    echo round(memory_get_usage() / 1024, 4), 'Kb', PHP_EOL; // 240.1797Kb
    
    unset($a);
    
    echo round(memory_get_usage() / 1024, 4), 'Kb', PHP_EOL; // 219.3125Kb
    

    The values vary depending on the system and version used of PHP, also varies if using extensions such as Opcache or XCache

  • memory_get_peak_usage returns the memory peak spent so far, then

    <?php
    
    echo round(memory_get_peak_usage() / 1024, 4), 'Kb', PHP_EOL; // 222.8906Kb
    
    $a = str_repeat("Hello", 4242);
    
    echo round(memory_get_peak_usage() / 1024, 4), 'Kb', PHP_EOL; // 241.7422Kb
    
    unset($a);
    
    echo round(memory_get_peak_usage() / 1024, 4), 'Kb', PHP_EOL; // 241.8047Kb
    

    See that the numbers remain and always increase.

Testing at the end of the script

There is a function called register_shutdown_function which runs at the end of the script, would be like a "__destruct ", at this point it is interesting to check the memory peak, thus:

<?php

register_shutdown_function(function() {
    echo PHP_EOL, round(memory_get_peak_usage() / 1024, 4), 'Kb';
});

$a = str_repeat("Hello", 4242);

unset($a);

So whenever the larger script is terminated (those that are included do not trigger this function unless there is a exit; or die('foo');) and will add to the end of the output/output/body of the document the result of how much was spent.

Note: register_shutdown_function may be added at any time before the shutdown

Here about extensions

  • Guilherme can paste this answer here too: http://answall.com/questions/178312/qual-%C3%A9-a-difference%C3%A7a-entre-memory-get-Usage-e-memory-get-Peak-Usage :P

  • 1

    @Miguel I think the other is dup this the moment I answered, I don’t think it’s cool to replicate the answer

  • It is not duplicated. The answers can be duplicated, the question does not. Anyway, it would be nice if in the other question they detail more (if you have what to detail). So here you would be with the superficial explanation, and there, with the deep.

  • William, I was joking. Surely you wouldn’t do that

2

The function memory_get_usage returns the number of bytes allocated to the script.

An example taken from the PHP documentation:

// É apenas um exemplo os números serão diferentes dependendo do sistema
echo memory_get_usage() . "\n"; // 36640

$a = str_repeat("Hello", 4242);

echo memory_get_usage() . "\n"; // 57960

unset($a);

echo memory_get_usage() . "\n"; // 36744

2

memory_get_usage:

memory_get_usage() returns the amount of byte memory that is being used by your php script.

DEMONSTRATION:

echo memory_get_usage(); // 112388 (bytes)

If you put the argument as true (memory_get_usage(true)), is returned the total memory reserved on the system for your php script:

DEMONSTRATION, with argument input as true:

echo memory_get_usage(true); // 262144 (bytes)

So if you want the real memory that the system is using you should do as the last demo above, memory_get_usage(true)

And if you want to know the real memory that your script consumed (within the reserved memory, obviously) use the first example.

  • The question refers to measuring the amount of memory used in a specific script. Thus the use of the argument real_usage does not suit in this case.

  • @fernandosavio, in that case you can use my first example, without true, this was to complement the answer by clarifying what the function can do

  • @fernandosavio when PHP is included in another all add together with the emalloc() (read about the API), or it may be until you can get it by script, but it will have to apply one to one what could be done at the time of the incluide and anyway the interpretation of the script occurs first that the execution and a call of a function in a different script does not define the weight of where it was declared, but of the execution consumption, in my view Miguel’s answer is correct.

  • No way I thought the answer was incorrect. To be honest I think it’s more correct than mine. I only raised the question more by the question anyway, because I did not add details about the function argument so that it fits better in his answer. Still thank you for explaining about the emalloc().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.