FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


know how much memory are your php scripts using





anticitizen1
well managing memory is an important thing making good php scripts.

this little script will who you how many memory is your script using:

In the very beggining of you code (or the initial file, if you include more than a single file)
Code:

$start_mem = function_exists('memory_get_usage') ? memory_get_usage() : 0;


what this code does?
it creates a new variable ONLY if the php fuction memory_get_usage exists, if not $start_mem will be equal to zero.

and very ending of your script add this lines:
Code:

if (function_exists('memory_get_usage') && $start_mem > 0) {
   $total_mem = memory_get_usage()-$start_mem;
   $memory_usage = '<br />Memory Usage: '.(($total_mem >= 1048576) ? round((round($total_mem / 1048576 * 100) / 100), 2).' MB' : (($total_mem >= 1024) ? round((round($total_mem / 1024 * 100) / 100), 2).' KB' : $total_mem.' Bytes'));
      }

echo $memory_usage;


it will show something like this:

Memory Usage: 1.58 MB

if you are using a cms like phpnuke, maybe the best place to add the firt code is in the index.php file or the modules.php file.
and the second segment of code to the footer file, but dont forget add the $start_mem as global.

[code]global $start_mem;[/code

this code only works if php is compiled with the --enable--memory-limit option.


if your script is using a lot of memory, consider to use trough the script.
[code]unset($variable_name);[/code]

where $variable_name could be a number, a boolean, an array or just an string.
Mgccl
you know you can use that to work with the declar structure and create a much more powerful profiling system that record the memory use of each step of the code Smile
Stubru Freak
anticitizen1 wrote:
Code:

if (function_exists('memory_get_usage') && $start_mem > 0) {
   $total_mem = memory_get_usage()-$start_mem;
   $memory_usage = '<br />Memory Usage: '.(($total_mem >= 1048576) ? round((round($total_mem / 1048576 * 100) / 100), 2).' MB' : (($total_mem >= 1024) ? round((round($total_mem / 1024 * 100) / 100), 2).' KB' : $total_mem.' Bytes'));
      }

echo $memory_usage;


The echo $memory_usage; should be in the if block, like this:
Code:

if (function_exists('memory_get_usage') && $start_mem > 0) {
   $total_mem = memory_get_usage()-$start_mem;
   $memory_usage = '<br />Memory Usage: '.(($total_mem >= 1048576) ? round((round($total_mem / 1048576 * 100) / 100), 2).' MB' : (($total_mem >= 1024) ? round((round($total_mem / 1024 * 100) / 100), 2).' KB' : $total_mem.' Bytes'));


echo $memory_usage;
      }


Otherwise this will allow a hacker to enter any html code they want (e.g. cookie-stealing code) into your page when register_globals is on. The user just has to click a bad link to your website.
mathiaus
Stubru Freak wrote:
Otherwise this will allow a hacker to enter any html code they want (e.g. cookie-stealing code) into your page when register_globals is on. The user just has to click a bad link to your website.

If $memory_usage had not been declared and globals was on, that would be true (to an extent). Don't forget though that at the top of your page, you do define that variable, either as the function return or 0 (if the function does not exist).
Stubru Freak
mathiaus wrote:
Stubru Freak wrote:
Otherwise this will allow a hacker to enter any html code they want (e.g. cookie-stealing code) into your page when register_globals is on. The user just has to click a bad link to your website.

If $memory_usage had not been declared and globals was on, that would be true (to an extent). Don't forget though that at the top of your page, you do define that variable, either as the function return or 0 (if the function does not exist).


He defines $start_mem, not $memory_usage (unless I'm missing something). Of course it only works when register_globals is on and memory_get_usage doesn't exist.
Related topics
Php and Other Wonderful Newbie Stories for Help.
How To : Improve Your PHP Programming
Tutorial: PHP Installed Modules Dynamic Reference Tool
Phoenix free PHP scripts
Using PHP
PHP Memory on Server 2
Max memory php / mysql
mysql backup
How to test PHP
need php scripts installed - 120 Frih$'s
PHP scripts
PHP scripts not functioning.
unable to send an email
Using PHP code from a Database
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.