views:

82

answers:

1

Hi guys I'm trying to optimize my application here and have started to benchmark snippets of code as to how much memory they are taking up. I just discovered that a single include statement is taking up to 1.5MB of memory. I'm using memory_get_usage() to check for memory used before adn after a snippet of code.

The file included includes just one file but if I try to include that nested include file on my own the maximum reduction is just 768KB for one include. The files are really small here and the only code in the files are class definitions.

What is happening here is this normal? And how can I resolve this strain. My application is hosted on a shared host and I'm wondering if this may be the cause of my application dying out so much.

EDIT:

This is how I am benchmarking:

$m = memory_get_usage(true);
include('/...');
$m = memory_get_usage(true)-$m;
echo $m;//over 1.5 MB ?
+2  A: 

That doesn't sound unusual to me, particularly if the include file is doing anything. PHP code is being interpreted on-the-fly, so the PHP interpreter needs to allocate a bunch of resources in order to parse and run the code in that file. 1.5MB may seem like a lot, but the PHP interpreter tends to be reasonable memory-hungry. For verification purposes, you could try including another empty file and seeing if that triggers any memory increase. You test other versions of the same include file with various things taken out to see if this is some kind of constant memory increase or if it's dependent on the functionality present. In any case, it doesn't sound sound out-of-place or unusual to me. It's most likely just the result of the interpreter allocating memory to read the included file, build some kind of AST for it and pre-allocate memory for all of the objects, values and variables in it.

Gian
The thing is that the snippet of code I benchmarked is called quite regularly and is it possible to maintain a cahce of included files like this? The file included is just a class definition to begin with.Actually my problem is that my application is hosted on an online server - and with more requests made it seems to die out and I get a Network error so I'm wondering if my system is too memory intensive or so? It works fine. albeit slow on my localhost though...
Ali
I don't know - does it access databases or files or make network requests or anything? It really depends what the code is doing, but if it's not doing any of those things, it seems strange that it is slow or running out of memory. Perhaps you can describe what it is doing a little more?
Gian
@Ali: "The thing is that the snippet of code I benchmarked is called quite regularly and is it possible to maintain a cahce of included files like this?" Install a PHP OpCode cache, such as APC: http://pecl.php.net/package/APC 3.0.19 is the latest stable release.
R. Bemrose