How to optimize and use LESS ram ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

How to optimize and use LESS ram ?

Post 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... )
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hard to say without seeing and understanding the code.
User avatar
cj5
Forum Commoner
Posts: 60
Joined: Tue Jan 17, 2006 3:38 pm
Location: Long Island, NY, USA

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to optimize and use LESS ram ?

Post 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.
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Re: How to optimize and use LESS ram ?

Post 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 ).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you intend to run it without using the output, don't have output.
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post 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....
Post Reply