Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate memory usage in PHP

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.

like image 250
foo.help Avatar asked May 20 '11 13:05

foo.help


People also ask

How do I monitor PHP memory usage?

The memory_get_usage function can be used to track the memory usage. The 'malloc' function is not used for every block required, instead a big chunk of system memory is allocated and the environment variable is changed and managed internally.

How much memory does each PHP process consume?

Memory Limit is Not The Same as RAM While RAM is the total available memory, the memory limit is per PHP process. That means that your site can consume e.g 10 GB of RAM, with a memory limit of 256 MB.

How much RAM does PHP need?

The right one for you depends on your system configuration. A typical appropriate memory limit for PHP running Drupal is 128MB per process; for sites with a lot of contributed modules or with high-memory pages, 256MB or even 512MB may be more appropriate.

How can I get CPU and memory usage in PHP?

To get the current memory usage, we can use the memory_get_usage() function, and to get the highest amount of memory used at any point, we can use the memory_get_peak_usage() function.


1 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.

like image 159
Yoshi Avatar answered Oct 04 '22 17:10

Yoshi