2

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();
3
  • Posting some of your code would be helpful; otherwise, it's mostly a guessing game. Commented Dec 16, 2009 at 14:24
  • 1
    CPU usage? Memory leakage? Which is it? Commented Dec 16, 2009 at 14:25
  • Posting the code IN THE QUESTION would be slightly more helpful. Commented Dec 16, 2009 at 15:08

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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..
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.
5

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?

2 Comments

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!
No.. Im sure it is only happening once.. plz have a look into my code
2

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?

3 Comments

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
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 ?
might be worth a separate question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.