Tom Boutell | 22 May 16:06 2012

Re: [PHP-DEV] memory usage ouchy

Actually, I just updated Rasmus' demo program to use assocative arrays
instead of objects.

In PHP 5.4.x, the associative array version uses more memory than the
object oriented version.

That's because PHP 5.4.x is using a flat array for predeclared
properties, as was mentioned earlier by Gustavo.

Associative arrays:

524288 bytes


262144 bytes

The object-oriented version is also faster, by about 20%.

Interestingly these results don't change much if I make the property
names/keys much shorter. Probably there's a minimum allocation of 64
bytes for these or something.

It would appear there is no longer a penalty simply for using many
objects vs. many associative arrays in PHP 5.4. The opposite, in fact.
I'm sure arrays didn't get slower, but objects now take advantage of
some optimizations that become possible when properties are

However this doesn't mean that calling lots of setters will
necessarily be as fast as direct property access... oh what the heck,
let's test that too:

Calling dead-simple setters for the four properties rather than
setting them directly slows down the OOP version to the point where it
runs at just about the same speed as the associative array version.
That's not terrible. Direct property access is still fastest (after
all that's what the setters do after they pay the overhead of the
function call).

On Mon, May 21, 2012 at 9:23 PM, Richard Lynch <ceo <at>> wrote:
> No offense intended, but if you've got so many OOP objects flying
> around that they are sucking down that much memory...
> You probably need to refactor your code and just "don't do that"
> Just my opinion.
> --
> brain cancer update:
> Donate:
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit:

Tom Boutell
P'unk Avenue
215 755 1330


PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: