Jump to content

Recommended Posts

Hello there! I got a little problem with a UDP server. It crashes due to a memory leak, and I am not experienced enough to figure out why.

 

Here is the code:

<?php

ini_set( 'display_errors', true );
ini_set('memory_limit', '1000000M');

class UDP_Server {

	protected $_socket = null;
	protected $_host = '';
	protected $_port = 0;
	protected $_clients = array();
	protected $_debug = false;

	public function __construct( $host = '', $port = 0 ) {
		$this->_host = $host;
		$this->_port = $port;
		$this->_socket = $this->_create_udp_server( $host, $port );
	}

	public function set_debug( $value = false ) {
		$this->_debug = $value;
	}

	public function process() {
		$buffer = stream_socket_recvfrom( $this->_socket, 1024, 0, $remote_host );
		if( empty( $buffer ) ) {
			return;
		}

		if( $this->_debug ) {
			echo $remote_host, ': ', $buffer, "\n";
		}

		if( strpos( $buffer, 'udp.register.ip' ) !== false ) {
			if( ! in_array( $remote_host, $this->_clients ) ) {
				$this->_clients[] = $remote_host;
			}

			stream_socket_sendto( $this->_socket, 'udp.register.complete', 0, $remote_host );
			return;
		}

		foreach( $this->_clients as $client ) {
			if( $client === $remote_host ) {
				continue;
			}
			stream_socket_sendto( $this->_socket, $buffer, 0, $client );
		}
	}

	public function __destruct() {
		fclose( $this->_socket );
	}

	protected static function _create_udp_server( $host = '0.0.0.0', $port = 0 ) {
		$address = 'udp://' . $host . ':' . $port;
		$socket = stream_socket_server( $address, $error_number, $error_message, STREAM_SERVER_BIND );
		if( ! $socket ) {
			die( 'could not create UDP server for ' . $address . '; Reason: [' . $error_number . '] - ' . $error_message );
		}

		stream_set_blocking( $socket, 0 );
		return $socket;
	}

}

$at_data_server  = new UDP_Server( '0.0.0.0', 5556 );
//$nav_data_server = new UDP_Server( '0.0.0.0', 5554 );
$at_data_server->set_debug( true );

while( 1 ) {
	$at_data_server->process();
	//$nav_data_server->process();
}

And here is the error log:


C:\Users\Jaran\Desktop>d:

D:\>cd xampp

D:\xampp>cd htdocs

D:\xampp\htdocs>php udp-server.php
AT*PCMD=29259,0,0,0,0,0T*REF=29258,0
AT*PCMD=29261,0,0,0,0,0T*REF=29260,0
AT*PCMD=29263,0,0,0,0,0T*REF=29262,0
AT*PCMD=29265,0,0,0,0,0T*REF=29264,0
AT*PCMD=29267,0,0,0,0,0T*REF=29266,0
AT*PCMD=29269,0,0,0,0,0T*REF=29268,0
AT*PCMD=29271,0,0,0,0,0T*REF=29270,0
AT*PCMD=29273,0,0,0,0,0T*REF=29272,0
AT*PCMD=29275,0,0,0,0,0T*REF=29274,0
AT*PCMD=29277,0,0,0,0,0T*REF=29276,0
AT*PCMD=29279,0,0,0,0,0T*REF=29278,0
AT*PCMD=29281,0,0,0,0,0T*REF=29280,0
AT*PCMD=29283,0,0,0,0,0T*REF=29282,0
AT*PCMD=29285,0,0,0,0,0T*REF=29284,0
AT*PCMD=29287,0,0,0,0,0T*REF=29286,0
AT*PCMD=29289,0,0,0,0,0T*REF=29288,0
AT*PCMD=29291,0,0,0,0,0T*REF=29290,0
AT*PCMD=29293,0,0,0,0,0T*REF=29292,0
AT*PCMD=29295,0,0,0,0,0T*REF=29294,0
AT*PCMD=29297,0,0,0,0,0T*REF=29296,0
AT*PCMD=29299,0,0,0,0,0T*REF=29298,0
AT*PCMD=29301,0,0,0,0,0T*REF=29300,0
AT*PCMD=29303,0,0,0,0,0T*REF=29302,0
AT*PCMD=29305,0,0,0,0,0T*REF=29304,0
AT*PCMD=29307,0,0,0,0,0T*REF=29306,0
AT*PCMD=29309,0,0,0,0,0T*REF=29308,0
AT*PCMD=29311,0,0,0,0,0T*REF=29310,0
AT*PCMD=29313,0,0,0,0,0T*REF=29312,0
AT*PCMD=29315,0,0,0,0,0T*REF=29314,0
AT*PCMD=29317,0,0,0,0,0T*REF=29316,0
AT*PCMD=29319,0,0,0,0,0T*REF=29318,0
AT*PCMD=29321,0,0,0,0,0T*REF=29320,0
AT*PCMD=29323,0,0,0,0,0T*REF=29322,0
AT*PCMD=29325,0,0,0,0,0T*REF=29324,0
AT*PCMD=29327,0,0,0,0,0T*REF=29326,0
AT*PCMD=29329,0,0,0,0,0T*REF=29328,0
AT*PCMD=29331,0,0,0,0,0T*REF=29330,0
AT*PCMD=29333,0,0,0,0,0T*REF=29332,0
91.131.53.24:56100: udp.register.ip
AT*PCMD=29335,0,0,0,0,0T*REF=29334,0
AT*PCMD=29337,0,0,0,0,0T*REF=29336,0
AT*PCMD=29339,0,0,0,0,0T*REF=29338,0
AT*PCMD=29341,0,0,0,0,0T*REF=29340,0
AT*PCMD=29343,0,0,0,0,0T*REF=29342,0
AT*PCMD=29345,0,0,0,0,0T*REF=29344,0
AT*PCMD=29347,0,0,0,0,0T*REF=29346,0
AT*PCMD=29349,0,0,0,0,0T*REF=29348,0
AT*PCMD=29351,0,0,0,0,0T*REF=29350,0
AT*PCMD=29353,0,0,0,0,0T*REF=29352,0
AT*PCMD=29355,0,0,0,0,0T*REF=29354,0
AT*PCMD=29357,0,0,0,0,0T*REF=29356,0
AT*PCMD=29359,0,0,0,0,0T*REF=29358,0
AT*PCMD=29361,0,0,0,0,0T*REF=29360,0
AT*PCMD=29363,0,0,0,0,0T*REF=29362,0
AT*PCMD=29365,0,0,0,0,0T*REF=29364,0
AT*PCMD=29367,0,0,0,0,0T*REF=29366,0
AT*PCMD=29369,0,0,0,0,0T*REF=29368,0
AT*PCMD=29371,0,0,0,0,0T*REF=29370,0
AT*PCMD=29373,0,0,0,0,0T*REF=29372,0
AT*PCMD=29375,0,0,0,0,0T*REF=29374,0
AT*PCMD=29377,0,0,0,0,0T*REF=29376,0
AT*PCMD=29379,0,0,0,0,0T*REF=29378,0
AT*PCMD=29381,0,0,0,0,0T*REF=29380,0
AT*PCMD=29383,0,0,0,0,0T*REF=29382,0
AT*PCMD=29385,0,0,0,0,0T*REF=29384,0
AT*PCMD=29387,0,0,0,0,0T*REF=29386,0
AT*PCMD=29389,0,0,0,0,0T*REF=29388,0
AT*PCMD=29391,0,0,0,0,0T*REF=29390,0
AT*PCMD=29393,0,0,0,0,0T*REF=29392,0

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate
 79 bytes) in D:\xampp\htdocs\udp-server.php on line 25

D:\xampp\htdocs>pause
Trykk en tast for å fortsette...

Does anyone have any clue on how I can fix this?

Link to comment
https://forums.phpfreaks.com/topic/297093-memory-leak/
Share on other sites

Tip: use -1 for the memory limit if you don't want a memory limit.

 

1. Where are those AT* lines coming from?

2. Have you used something like memory_get_usage to see where the memory increase comes from?

3. Why did you disable blocking? PHP will soak up CPU until a message comes in. Leave blocking off if you must, but use stream_select to wait for a message to arrive, optionally with a timeout, and only try to recv a message once that returns activity (by looking at the modified $read parameter).

Link to comment
https://forums.phpfreaks.com/topic/297093-memory-leak/#findComment-1515199
Share on other sites

Tip: use -1 for the memory limit if you don't want a memory limit.

 

1. Where are those AT* lines coming from?

2. Have you used something like memory_get_usage to see where the memory increase comes from?

3. Why did you disable blocking? PHP will soak up CPU until a message comes in. Leave blocking off if you must, but use stream_select to wait for a message to arrive, optionally with a timeout, and only try to recv a message once that returns activity (by looking at the modified $read parameter).

 

 

I did not create the code above, it is made by this guy: https://carldanley.com/controlling-the-ar-drone-over-lte/

 

It is a part of what I need to control my AR Drone through LTE/4G network.

 

If I removes the memory limit won't that cause some problems?

 

As I did not create the script I can't answear your other questions. I am pretty basic at PHP ::)

 

EDIT: Tried to remove the memory limit, but the scripts does not run for even a minute before it is out of memory.

Edited by Jaran
Link to comment
https://forums.phpfreaks.com/topic/297093-memory-leak/#findComment-1515200
Share on other sites

If I removes the memory limit won't that cause some problems?

The code already tries to do that with

ini_set('memory_limit', '1000000M');
That's about a terabyte.

 

EDIT: Tried to remove the memory limit, but the scripts does not run for even a minute before it is out of memory.

It was suggestion only to modify that ugly ini_set() line. The memory problem will still be there.
Link to comment
https://forums.phpfreaks.com/topic/297093-memory-leak/#findComment-1515237
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.