Page 1 of 1

PHP Script begins with 15MB already in use

Posted: Wed Feb 09, 2011 8:40 am
by tomhand
I added a new file to our site that just prints out the memory used and before anything has happened memory_get_usage() says there is already 15MB in use. This only happens when I call it from a web browser. When I run the script from the command line, I get a much more reasonable 86KB.

Can anyone think of what might cause this???

Code: Select all

<?
echo memory_get_usage();
echo "<br/><br/>GLOBALS: ".strlen(serialize($GLOBALS))." bytes<br/>";
echo "_SERVER: ".strlen(serialize($_SERVER))." bytes<br/>";
echo "_GET: ".strlen(serialize($_GET))." bytes<br/>";
echo "_POST: ".strlen(serialize($_POST))." bytes<br/>";
echo "_FILES: ".strlen(serialize($_FILES))." bytes<br/>";
echo "_COOKIE: ".strlen(serialize($_COOKIE))." bytes<br/>";
echo "_SESSION: ".strlen(serialize($_SESSION))." bytes<br/>";
echo "_REQUEST: ".strlen(serialize($_REQUEST))." bytes<br/>";
echo "_ENV: ".strlen(serialize($_ENV))." bytes<br/>";
echo "get_defined_vars: ".strlen(serialize(get_defined_vars())). "bytes<br/>";
echo "get_defined_constants: ".strlen(serialize(get_defined_constants()));
results in

[text]
15091064
GLOBALS: 15732 bytes
_SERVER: 3326 bytes
_GET: 6 bytes
_POST: 6 bytes
_FILES: 6 bytes
_COOKIE: 1616 bytes
_SESSION: 2 bytes
_REQUEST: 1616 bytes
_ENV: 1179 bytes
get_defined_vars: 23597 bytes
get_defined_constants: 32605
[/text]

Thanks for your help!
-Tom

Re: PHP Script begins with 15MB already in use

Posted: Wed Feb 09, 2011 2:37 pm
by pickle
This file isn't being include()d or require()d from another file?

You can set PHP up to automatically include() or require() files at the beginning of each request - might these be adding memory usage?
You can also call get_declared_classes() and get_defined_vars() to see if there's anything custom in there.

Re: PHP Script begins with 15MB already in use

Posted: Wed Feb 09, 2011 3:20 pm
by tomhand
Nope, I'm just calling this script directly.

get_declared_classes and get_defined_vars seem benign (and, as can be seen above, can be serialized down to only 60KB).

Good idea about the auto included files. I added a phpinfo() call to that test page and both auto_prepend_file and auto_append_file are not set.

Thank you. Any other ideas on what may be going on?

Re: PHP Script begins with 15MB already in use

Posted: Wed Feb 09, 2011 4:00 pm
by pickle
get_defined_vars() and get_declared_classes() only returns an array of variable names and class names. The idea was, if you output get_declared_classes() and look through the list, you might see some user classes that aren't part of the PHP core. That would indicate they were being included somehow.

Other than that, I have no ideas.