PHP and memory use

I'm using a php script on a shared server to generate photo galleries on
a site. The script was today blocked by the system, with the error
message that the allowed memory size of 40MB was exhausted. I had to ask
the host to increase this limit. They did it and the script now works
again.

However they said that they cannot allocate more than 65MB per PHP
script and it is possible that in the future, when my site grows further
I will again hit the limit. Is there a way to control how much memory a
PHP script is using? How to reduce the amount of memory needed?
--

Alfred Molon
Courtney [ Sa, 21 Juli 2007 01:21 ] [ ID #1774947 ]

Re: PHP and memory use

On Sat, 21 Jul 2007 01:21:12 +0200, Alfred Molon <a [at] b.c> wrote:
> I'm using a php script on a shared server to generate photo galleries on
> a site. The script was today blocked by the system, with the error
> message that the allowed memory size of 40MB was exhausted. I had to ask
> the host to increase this limit. They did it and the script now works
> again.
>
> However they said that they cannot allocate more than 65MB per PHP
> script and it is possible that in the future, when my site grows further
> I will again hit the limit. Is there a way to control how much memory a
> PHP script is using? How to reduce the amount of memory needed?

In gallery script, it's usually the resizing (or opening) of images that
require a lot of peak memory. The overall memory usage is usually pretty
low. If it's the resizing that does it, it's not very likely the memory
usage will increase dramatically any further, the peaks will usually be
around the same, allbeit maybe more often. Don't forget the destroy()
statements en save/cache the resized images, and I'd think you'd be
allright.
--
Rik Wasmus
luiheidsgoeroe [ Sa, 21 Juli 2007 01:42 ] [ ID #1774949 ]

Re: PHP and memory use

On Jul 20, 4:21 pm, Alfred Molon <a... [at] b.c> wrote:
>
> Is there a way to control how much memory a PHP script is using?

Of course. Don't store in variables anything that doesn't need to be
stored. Plan the database and file system interaction accordingly.
Output stuff as soon as it's ready to be output. Don't hesitate to
unset() temporary variables with a lot of data in them as soon as
you're done with them. Avoid resizing images dynamically; resize them
once at the time of upload and serve the resulting thumbnails
statically.

> How to reduce the amount of memory needed?

By design, of course. I would hazard a guess that your problem has
something to do with page content being formed inside a loop and then
dumped in one fell swoop. Something like this:

$content = '';
// ...
while ($record = mysql_fetch_array($result)) {
$content .= "<img src='{$record['imageURL']}'>";
}
// ...
echo $content;

Consider outputting content immediately:

while ($record = mysql_fetch_array($result)) {
echo "<img src='{$record['imageURL']}'>";
}

Another possibility is that your memory issues come from resizing
large images. If that's the case, there's really nothing you can do,
other than limit the size of images you can upload...

Cheers,
NC
nc [ Sa, 21 Juli 2007 01:51 ] [ ID #1774952 ]

Re: PHP and memory use

..oO(NC)

>On Jul 20, 4:21 pm, Alfred Molon <a... [at] b.c> wrote:
>
>> How to reduce the amount of memory needed?
>
>By design, of course. I would hazard a guess that your problem has
>something to do with page content being formed inside a loop and then
>dumped in one fell swoop. Something like this:

I don't think so.

>$content = '';
>// ...
>while ($record = mysql_fetch_array($result)) {
> $content .= "<img src='{$record['imageURL']}'>";
>}
>// ...
>echo $content;

I use this extensively in my own framework, simply because there's no
other way for me to perform particular tasks (filtering and output
caching for example). But even when creating long and complex pages with
a lot of nested objects, each storing its own output buffer, this has
never become a memory issue for me. My most complex page until now (with
55 database queries, 111 loaded classes, 324 objects created while
script execution, 344KB final HTML code) just consumes 8.1MB.

>Another possibility is that your memory issues come from resizing
>large images.

I'm pretty sure that this is the reason, but without knowing some code
it's impossible to say. It might help to call memory_get_usage() from
time to time to see where the real hole is.

>If that's the case, there's really nothing you can do,
>other than limit the size of images you can upload...

"Please don't upload 10 megapixel images." ;)

Micha
Michael Fesser [ Sa, 21 Juli 2007 03:11 ] [ ID #1774953 ]

Re: PHP and memory use

Thanks for all the tips. unset and memory_get_usage were the solution
(didn't know about these two functions).

I managed to track the memory usage with memory_get_usage and used unset
to dispose of unnecessary variables. The peak memory usage is now down
to 21MB from 41MB.

--

Alfred Molon
Courtney [ So, 22 Juli 2007 00:00 ] [ ID #1774964 ]
PHP » alt.php » PHP and memory use

Vorheriges Thema: PHP does not recognize PECL classes/functions
Nächstes Thema: Newbie question about UNIQUE MySQL v5.0.22