NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP and memory usage

Hans Zaunere hans at nyphp.com
Sun Oct 24 19:14:39 EDT 2004


> This is a very general question, but I don't know where to begin, but
> generally.  I have, over the years built a fairly complex publishing
> system which is installed on several sites.  Recently, I've noticed
> that my mailing list component is choking; PHP bunks out with a
> memory limit error, which is very unfortunate, since there's no way
> of knowing how many messages were sent before this happened.

As someone mentioned, there is a memory limit directive in php.ini that
could eliminate this error right off the bat.

> I need to address this issue, but I wanted to get people's strategic
> opinions about what to cut out in order to reduce memory consumption.
> 
> Basically, my central data class extracts names and email addresses
> out of a database, stores that in an array.  We're talking around
> 6,000 users here...not a huge number, but sizable.
> 
> Then, I loop through the returned users, create a custom message for
> them by replacing codes in a template, and pass it off to a class of
> mine which is just a wrapper for PHPMailer.

That sounds about right.  You could use the LIMIT clause, although there
is a chance that you get the same row twice.  This might happen if a
DELETE/INSERT happens during your routines.  The other way to do it
would be to use a range of the primary key, or even an alphabetic range
(all 'a' users, then 'b', etc).

> That's the basic structure.
> 
> So, what should I do?  A few salient points:
> 
> : Do objects and arrays eat up a ton of memory?  Should I instead
> select each user separately inside the loop (more database
> connections, but fewer items stored each time)?

They do, but 6000 isn't really that much.  You could SELECT the users in
chunks, per above.

> : Should I be unsetting variables as soon as I no longer need them?
> Would this make much of a difference?

If you set the same variable to another value, and it's not referenced
from anywhere else, the Zend Engine should free the old memory block.
This applies to scalar variables and arrays, but NOT objects, or even
within objects and their properties, AFAIK (assuming this is PHP 4).

> : Anything else I should look at?  My goal here is to build it so it
> will scale upward; this project will eventually have tens of
> thousands of users...maybe hundreds of thousands.

The simple answer is to stay away from objects - they will eat memory,
unless numerous confusing ampersands are used.  I'd do this from a
simple function (not a method).  If you're using PHP 5, then this isn't
so much a concern.  Also, if this is MySQL you could use
mysql_unbuffered_query(), or mysqli_use_result() if it's MySQL 4.1.
That said, depending on how you're handling your PHP variables, this
might not make a difference at all.

H




More information about the talk mailing list