socket_recv and memory leak

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluestermann
Forum Newbie
Posts: 2
Joined: Mon Oct 26, 2009 12:33 pm

socket_recv and memory leak

Post by bluestermann »

Hello community!

I have a little problem and hope somebody here may help.

Currently I have a small cli-deamon, that connects himself via sockets to a system and asks for statistics. After the data has been received i close that socket, transform the data and write them into a database.
Then I do the same with the next system.... and so on.
I've monitored the process with "memory_get_usage" and it looks like "socket_recv" has a memory leak (at least like i have implemented it).
After each look the used memory gets bigger. If I dont read any data, I dont have that problem.

This is my code (simplified):

Code: Select all

 
while(true) {
    usleep(100);
    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    $sock_data = @socket_connect($sock,$host, $port);
    socket_write($sock, "some data please");
    /* tryed it with socket_read. Same problem.
    do {
        $recv = '';
        $recv = socket_read($sock, $buff, PHP_BINARY_READ);
        if($recv != '') {
            $readedContent .= $recv;
        }
    } while($recv != '');*/
    while (($curRecived = @socket_recv($sock, $recv, 8192, MSG_WAITALL))) {
        $readedContent .= $recv;
        $recived += $curRecived;
    }
    socket_close($sock);
    unset($sock);
    //transorming [...]
    //Cleanup
    unset($readedContent);
    unset($recv);
    unset($recived);
    unset($curRecived);
    //choose next connection [...]
}
 
Could anyone imagine, why this is happening?

Thanks for any help.

I'm running this script on RedHat Enterprise Linux 5.3 with PHP 5.1.6.

Update:
In PHP 5.2.9 the garbadge-collection works better. No memory leaks there.
If I change the usleep(100) into an sleep(1) I can observe, that the GC in 5.1.6 works, but mutch slower.
So there doesn't seem any answer to my problem beside upgrading. If anyone else has some other ideas, please feel wellcome to post them :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: socket_recv and memory leak

Post by requinix »

bluestermann wrote:Update:
In PHP 5.2.9 the garbadge-collection works better. No memory leaks there.
If I change the usleep(100) into an sleep(1) I can observe, that the GC in 5.1.6 works, but mutch slower.
So there doesn't seem any answer to my problem beside upgrading. If anyone else has some other ideas, please feel wellcome to post them :)
I took a quick look through the bug lists - didn't see anything - and there's too many revisions for me to check the changelogs, so...
If upgrading eliminated the problem then you certainly could have found a memory leak.


Without looking too closely at the code, I'm wondering whether you should be using socket_select somewhere...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: socket_recv and memory leak

Post by Weirdan »

One thing that you may not be aware of is the fact that even if errors/warning/notices are suppressed, they are still constructed (thus this could be where the memory is unexpectedly leaking). There is unset variable $readedContent that generate a notice on every iteration of the outer loop - it is unset because you do that near the end of the loop.
bluestermann
Forum Newbie
Posts: 2
Joined: Mon Oct 26, 2009 12:33 pm

Re: socket_recv and memory leak

Post by bluestermann »

Ah, right. Forgot to redeclare the variable.
Now no warnings(notice,...) are thrown anymore (checked via try/catch and own error reporting, which marked the missing redeclare), but there is still a little leak. Ok, after a few seconds the garbage collection kicks in and if the script isn't running that fast, there shouldn't be any problems (but it would be nice if it could run faster).

Sadly upgrading isn't posible here.

Thanks for your input!
Post Reply