Вопрос

While optimizing a site for memory, I noticed a leap in memory consumption while including a large number of PHP class files (600+) for a specific purpose. Taking things apart I noticed that including a PHP file (and thus presumably compiling to opcodes) takes about about 50 times more memory than the filesize on disk.

In my case the files on disk are together around 800 kB in size (with indentation and comments, pure class declarations, not many strings), however after including them all, memory consumption was around 40 MB higher.

I measured like this (PHP 5.3.6):

echo memory_get_usage(), "<br>\n";
include($file);
echo memory_get_usage(), "<br>\n";

Within a loop over the 600 files I can watch memory consumption grow from basically zero to 40 MB. (There is no autoloader loading additonal classes, or any global code or constructor code that is executed immediately, it's really the pure include only.)

Is this normal behaviour? I assumed opcodes are more compact than pure source code (stripping out all spaces and comments, or having for example just one or two instruction bytes instead of a "foreach" string etc.)?

If this is normal, is there a way to optimize it? (I assume using an opcode cache would just spare me the compile time, not the actual memory consumption?)

Это было полезно?

Решение

Apparently that's just the way it is.

I've retested this from the ground up:

  • Include an empty zero length file: 784 bytes memory consumption increase
  • Include an empty class X { } definition: 2128 bytes
  • Include a class with one empty method: 2816 bytes
  • Include a class with two empty methods: 3504 bytes

The filesize of the include file is under 150 bytes in all tests.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top