Home » Php » How to calculate memory usage in PHP

How to calculate memory usage in PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve written a very big PHP file with lots of variables and equations. Now I’m going to host it on my college website, but with our hosting there is a memory consumption limit. There will be thousands of hits to that page as soon as we link it.

So I want to know the consumed memory of my PHP file when it is requested once,so that we can calculate the usage of memory resources and based on that determine how to proceed.

How to&Answers:

Have a look at:

http://www.php.net/manual/en/function.memory-get-usage.php

and

http://www.php.net/manual/en/function.memory-get-peak-usage.php

maybe, these can help you.

Answer:

could be wrong but apachebench might be able to help you here. It’ll give you an idea of how your college servers will cope with the load.

http://httpd.apache.org/docs/2.0/programs/ab.html

Answer:

If you use an auto-loader for your classes, it might be useful to include this code at the beginning of the function:

function autoloader($class) 
{
    $memory = memory_get_usage (FALSE );
    print ("Autoloading $class - memory is $memory<br>");

  (autoloader code here)
}

As I develop my application, it gives me a rough idea about the memory my script is using. I never optimize my application until the very end so every class is loaded individually, which is definitely useful to see how much memory is being used as each one is loaded.

I hope this helps somebody 🙂