Forum Moderators: coopster
The program has worked great, except one problem. Over time as it is doing it's while loop and sleep it starts eating up more and more memory until the server crashed. We just finished shutting down this script after it had grown from .01 in PS to 14MB over a couple days.
Any ideas on how to stop it from using more memory, or what is making it use more memory?
while(1<2) {
exec("pwd", $pwdval); // Find where we are at in the system
foreach($pwdval as $pwdstr) $pwdres.=$pwdstr;
$pwdreg=str_replace("/", "\/", $pwdres); // Format for preg_match.
exec("ps -A ux", $ps_return); // Get PS results
foreach($ps_return as $psstr) $psres.=$psstr;
if(preg_match("/".$pwdreg."\/thisscript.php/", $psres)) { } else {
system("/usr/local/bin/php ".$pwdres."/thisscript.php > /dev/null &");
// FAKE BROWSER TYPE
ini_set("user_agent","Lilith/1.0;");
$dh = fopen("".$OpenURL."","rb"); // Connect to remote system
while (!feof($dh)) {
fread($dh, 8192);
}
fclose($dh);
}
sleep($sleeper); // Sleep for x seconds.
[edited by: l3vi at 11:43 pm (utc) on July 14, 2005]
jatar_k For our project it would require a large amount of work to use crontab as we would be adding 500+ entries a day as well as removing them at times. We needed something that was automated and would manage itself. This does the trick but is just building up memory over time.
I made some changes and checked PS after Jatar_k’s post. %MEM was 0.1 as of now it still is 0.1, don’t know how slow it is. One system that it is on builds up really fast in a couple days, on my system it took a week to get to 0.6 so testing is going to be slow.
Keep you all posted! ;)
Added code at command sleep()
sleep($sleeper);
// CLEAN UP VARS
unset($dh);
unset($pres);
unset($psstr);
unset($ps_return);
unset($pwdres);
unset($pwval);
unset($requestURL);
unset($pwdval2);
unset($pwdstr2);
unset($pwdres2);
unset($pwdreg2);
unset($CatName);
unset($array_total);
}
From what I see, after sleep() time the system starts the loop and builds up more memory.
So it must be something with fread, system.
As the out put is going to /dev/null system command would dump the output to /dev/null and that would be the end of that data. Unless /dev/null can build up a stack in memory till a program stops.
As we all know the data from fread can me set to a string var. Because Im not adding freads data to a var that would be my top choice if fread would store the data into memory as it cran the loop until the program is finished.
If the above does not fix it Im at the extent of my programming skills as my only guess would then be its something with how the system process php scripts that run indefinitely.
>> Unless /dev/null can build up a stack in memory till a program stops
I dont believe it can
I am going to have to replicate this and watch it
Obiously $pwdres gets megabytes in length after a while if you don't reset it. Now you do reset it (unset), but you still have the following line which is now the cause of the memory overflow:
foreach($ps_return as $psstr) $psres.=$psstr;
I recommend the following line instead:
$psres = join("",$ps_return);
And instead of
foreach($pwdval as $pwdstr) $pwdres.=$pwdstr;
I recommend:
$pwdres = join("",$pwdval);
Then you don't need to unset anything (but still unsetting would do no harm).
I hope you see that this is the problem: .= adds something to the string, and at each while-iteration the string gets longer and longer.
The simple solution was just to write a quick script that would active itself and run till it was gone as time and speed is what’s most important to us.
I will be removing sleep() while testing. ;)