PHP Script begins with 15MB already in use

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomhand
Forum Newbie
Posts: 2
Joined: Wed Feb 09, 2011 8:30 am

PHP Script begins with 15MB already in use

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP Script begins with 15MB already in use

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
tomhand
Forum Newbie
Posts: 2
Joined: Wed Feb 09, 2011 8:30 am

Re: PHP Script begins with 15MB already in use

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP Script begins with 15MB already in use

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply