我有一个使用flash和php开发的游戏网站.php代码包含4000行,它将作为cron运行.在代码内部,有一个while循环,它将无限运行,用于检查在套接字中写入的任何数据,并相应地调用不同的函数,并将结果发送回套接字.从闪存中,它将获得结果并将显示.
我面临的问题是,从PHP代码的某个地方,它是泄漏内存.由于它非常大,我无法从它发生的地方找到它.而且它只能作为一个cron运行.是否有任何工具可以找出内存泄漏?我听说过xdebug但我没用过.任何其他 ?
check.php(作为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";}
Run Code Online (Sandbox Code Playgroud)
server.php(只是部分)
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();
Run Code Online (Sandbox Code Playgroud)
两件事,首先,确保你在循环内至少睡一次,以确保你不使用97%的cpu.
其次,我发现的一个技巧是,如果有任何数据库活动,则调用mysql_free_result(或者它等同于其他DBMS')来释放用于存储查询结果的内存.