tags:

views:

150

answers:

3

Hi,

I have a game site developed using flash and php. The php code contains 4000 lines and it will run as a cron. Inside the code, there is one while loop which will run infinitely for checking any data is written in the socket and call different functions accordingly and will send the results back to the sockets. From flash, it will get the results and will be displayed.

The problem Im facing is, somewhere from the php code, it is leaking memory. Since it is very big, I can not find out from where it is happening. Moreover it can be run only as a cron. Is there any tool to find out the memory leakage ? I have heard about xdebug but I didnt use. Any other ?

check.php (as cron)

$sock = fsockopen(IP_ADDRESS, PORT, $sock_error_code, $sock_error_string, 10); if (!$sock){
      $message = "Server was down, restarting...\n\n";  
      $last_line = system("php -q gameserver/server.php", $retval);} else {
         $message = "Server is up...";
         $message .= $sock_error_string." (".$sock_error_code.")\n\n";}

server.php (only some part)

class gameserver {
var $server_running = true;
function gameserver() {
 global $cfg, $db;

 $this->max_connections = $cfg["server"]["max-connections"];

 $this->start_socket();

 echo "Gameserver initialized\n";
 while ($this->server_running) {
  $read = $this->get_socket_list();
  $temp = socket_select($read, $null, $null, 0, 15);
  if (!empty($read)) {
   $this->read_sockets($read);
  }
  $db->reconnection();
  $this->update_DB_records();
  $this->check_games_progress();

  if ($this->soft_shutdown && $this->active_games == 0) {
   $this->server_running = false;
   echo "soft shutdown complete\n";
  }
 }

 $this->stop_socket();

 echo "Server shut down\n";
}}        $server = new gameserver();
+5  A: 

Are you starting never-ending programs from CRON? Cron will start a new instance according to the schedule you specify and you'll end up with several running programs doing the same thing.. Could this be your problem?

Isak Savo
That makes complete sense... does it look like * * * * * if so you will try to run that cron every minute with an infinate loop called each time!
Lizard
No.. Im sure it is only happening once.. plz have a look into my code
binoy
+2  A: 

I am assuming that you are not starting a new instance using cron every minute, and having each run an infinite loop:

XDebug is probably your best bet. Other than that, you could use memory_get_usage() and log memory usage in specific points of your loop.

Could it simply be that your script accumulates data and doesn't clean it up properly on the end of each loop?

Pekka
No.. Im sure it is only happening once... ya, it may be accumulating data somewhere, but it is diificult to find out from 4000 lines
binoy
Well, welcome to the world of debugging.
Pekka
ok.. Even the following program showing 97% percentage of CPU usage if I run from the command prompt.. <?php $bool = true; while ($bool){ }?>Any way to free up the CPU usage frequently ?
binoy
might be worth a separate question.
Pekka
+4  A: 

Two things, first, ensure that you sleep at least once inside the loop, to ensure that you don't use 97% cpu.

Second, a trick I've found is, if there is any database activity, to call mysql_free_result (or it's equivalent for other DBMS') to free up the memory used to store the result of the query.

Kazar
I have added sleep(1) inside the loop, so now it is not using 97% of CPU. Hope this will not cause any other bottleneck issue later.. thanks for the reply..
binoy
You may want to consider how often your client is polling your server, and whether you can adjust the amount you sleep to further decrease the time spent running your script.
Kazar