When you free memory you are only marking memory as available for reclaiming, but it doesn't mean it will actually reclaim it at all. The garbage collector usually runs at a very low priority and if you don't give your process a break, it will never catch up. One thing I would try is to insert a call to sleep() within the loop and see if that gets the garbage collector to do some catching up (and if it does, please let us know).

Another thing would be to have a master process shelling out processes to process a template one at a time. It might either be either through exec() or using curl to request Apache to deal with handling the processes. The end of each individual process would free all its memory and, after all, that is the way PHP is meant to run and has been highly optimized to do. Using some shared memory to communicate whatever needs to be shared would avoid too much repetition. If the templates to be processed are large, perhaps you can get some external utility to process it far more efficiently.

Nevertheless, I wouldn't try any of that until I exahusted all that can be done on the programming side. Are you using file_get_contents to read whole chunks at a time? Is it really necesary? Can you manage to read a line at a time? If having the whole template in memory is required, can you break it up into an array with explode() and operate on each line at a time or whatever unit you have? Or better yeat, have it read a line at a time into an array and avoid the explode(). Memory gets a better chance of being reused if it is split into many smaller chunks. Using square brackets to access and specially modify individual characters as if the string is an array of characters works on the string right in place, avoiding copying and trashing the memory, though it might not be faster.

Anyway, these are no more than random thoughts and nothing beats a good application design, something that nowadays, with so much memory and powerful processors tends to be overlooked. Someone famous once said that 64kbytes was all the memory anyone would ever need.

Satyam

----- Original Message ----- From: "Arthur Erdös" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>; "Jochem Maas" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Thursday, April 12, 2007 3:13 PM
Subject: Re: [PHP] free allocated memory: HOW ?


I am using PHP version 5.1.2. on my dev machine. The same problem with PHP 5.2.1 on webserver.

I am generating mails based on a HTML Template. If you look at the appended screenshot you'll see that after reading the template with fread() memory_get_usage() says ~386 MB in use...

Then the placeholders in the read template are replaced by customized values within a loop. The size of the used memory grows permanently in each loop by approx 30 MB.

any ideas what i am doing wrong and where i [ab]use php? ^^


Jochem Maas schrieb:
Arthur Erdös wrote:
Hello all,

is there a way to free memory allocated by variables in PHP?? This is a
very important issue concerning long running scripts...

this is a recurrent problem - not much can be done about it AFAIK ... I'd
be very glad to be proved wrong.

I have a script that generates > 5000 Newsletters and when the script
finishes it uses 1.8 GB (!!) of RAM. Although I am using unset() to
clean up variables (tried with $var = null too).

okay this does suggest your doing something else very wrong - I personally have code that regularly generates upto 20,000 individualised newsletters without going anywhere near this ammount of memory - granted I've hit my memory limit a couple of times and had to jack it up to 128Megs (in practice the script grab just over
64Megs when it's doing it's worst).

what version of php are you [ab]using?

I've read sth about php not giving the allocated memory back to the OS
until a script finishes, is that right?? Is it possible to free the
memory "by hand"?

Any suggestion is appreciated!

thx in advance


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 269.2.0/757 - Release Date: 11/04/2007 17:14



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to