Include statement taking up 1.5 MB of memory?
Hi guys I'm trying to optimize my application here and have started to benchmark snippets of code as to how much 开发者_JAVA技巧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 ?
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.
精彩评论