Page 1 of 1

How to optimize and use LESS ram ?

Posted: Thu Apr 13, 2006 10:21 am
by Deseree
Hi,
I've got a script that loops quite alot of times and well it uses lots of ram... it's tasks is EXTREMELY simple and when i loop only a few times, it uses hardly any ram... so i'm wondering how to use less ram....the script loops many times and runs for hours until out of ram....

so.. i'm running some via cron, and others via screen, but lately i've been running only via cron since last reboot.

I need to know if this is anywhere in the ballbpark of being true....

my script outputs quite alot each time it recurses in the for loop, therefore... i think all this output is like being outputted siltenly since cron is running the script....






is that true? and if it is, that will use quite a bit of ram right?

how would i best minimize the use of ram, like cleaning the output so it's discarded immediately ( and therefore save ram yes or no? I hope i'm not way off base with a total n00b question... )

Posted: Thu Apr 13, 2006 10:35 am
by feyd
hard to say without seeing and understanding the code.

Posted: Thu Apr 13, 2006 11:02 am
by cj5
If you're looping a large array or just a large loop in general, and want to just find one item, and then forget the loop, use the break call

Re: How to optimize and use LESS ram ?

Posted: Thu Apr 13, 2006 11:04 am
by onion2k
Deseree wrote: I've got a script that loops quite alot of times and well it uses lots of ram... it's tasks is EXTREMELY simple and when i loop only a few times, it uses hardly any ram... so i'm wondering how to use less ram....the script loops many times and runs for hours until out of ram....
As Feyd says, it's difficult to say without seeing the code. However, it sounds like your script isn't cleaning up after itself properly. You should be calling unset(), mysql_free_result(), imagedestroy() etc when you've finished with resources. Also, make sure the script ends nicely even if something goes wrong .. liberal use of die() will help. If it doesn't, and cron is firing up a new process every so often, then eventually you'll have lots of them running and the server will die.

Re: How to optimize and use LESS ram ?

Posted: Thu Apr 13, 2006 11:10 am
by Deseree
onion2k wrote:
Deseree wrote: I've got a script that loops quite alot of times and well it uses lots of ram... it's tasks is EXTREMELY simple and when i loop only a few times, it uses hardly any ram... so i'm wondering how to use less ram....the script loops many times and runs for hours until out of ram....
As Feyd says, it's difficult to say without seeing the code. However, it sounds like your script isn't cleaning up after itself properly. You should be calling unset(), mysql_free_result(), imagedestroy() etc when you've finished with resources. Also, make sure the script ends nicely even if something goes wrong .. liberal use of die() will help. If it doesn't, and cron is firing up a new process every so often, then eventually you'll have lots of them running and the server will die.

exactly, i've got MANY of them running in "ps" showing right now, but how about the output?

What i'm moST worried about is the echo "" commands i'm sending text and html to the browser, and when i'm running in cron, WHERE does that output go?

IF I run the script via ssh, it outputs so much that after one hour, ONE browser ( IEXPLORER ) is upto 800 mb ram usage. now I've got 2 gb ram in my linux server and like 60 of these scripts running in "ps" right now, and the ram usage ATM is 1.5 gb ram, BUT sometimes it spikes to 2.5 gb ram ( 500-600 mb from swap ).

Posted: Thu Apr 13, 2006 11:18 am
by feyd
If you intend to run it without using the output, don't have output.

Posted: Thu Apr 13, 2006 11:26 am
by Deseree
feyd wrote:If you intend to run it without using the output, don't have output.
k yea, i just added a global variable so i can turn off output when using via cron!!!


as far as this goes....

Code: Select all

unset()
mysql_free_result()
imagedestroy()

die()
what are the rest of them, like is there a full list anywhere?

And the command

Code: Select all

exit;
I have this at the end of my script. ( IT is not a for loop looking for something, I know about break; ) for that case!

I've also got everything separated into functions, and i believe after a function is finished running, it destroy's all data right? Since everything in a function is local...

and my sql statements are all $sql = , so i don't think unset is needed since each time sql calls, it overwrites the same variable, correct me if i'm wrong.

also, if i put ob_start() at the beginning of the script, and then ob_clean() every for loop...... do you get where i'm heading, is ob_ going to work throughout the main and functions?

like:

Code: Select all

ob_start() // AT THE ABSOLUTE BEGINNING

here is the main coding....

do_this_1000_times()

blah blah blah

morefucntions()

etc...

ob_end_clean() // I think this wipes any output and ends the ob call without outputting anything..... . but everything was still ran just OUTPUT was suppressed right? any filecreation i had going did ok right?, wasn't effected?

fucntion do_this_1000_times()
{
ob_clean();

do whatever you need here

ob_clean();
}


rest of fuctions down here....