mgdm / Mosquitto-PHP

A wrapper for the Eclipse Mosquitto™ MQTT client library for PHP.
BSD 3-Clause "New" or "Revised" License
529 stars 146 forks source link

Memory garbage collection #32

Open toan-leminh opened 8 years ago

toan-leminh commented 8 years ago

I do stress test below:

mgdm commented 8 years ago

Hi, Any chance I could see the script you're using to test? I don't see the increase in PHP 5.6.10, using this script:

<?php
$c = new Mosquitto\Client();
$c->onConnect(function() use ($c) {
    $c->subscribe('#', 0);
});
$c->onMessage(function() {
    static $i = 0;
    $i++;
    if ($i % 10 == 0) {
        var_dump(memory_get_usage());
    }
});

$c->connect('my.broker');
$c->loopForever();
toan-leminh commented 8 years ago

Hi, I don't use memory_get_usage function, I checked directly on my VPS server using "top" or "ps aux --sort -rss" command and saw the increasing on memory of process. Here is my code:

<?php
$brokerInfo = array(
    'ip' => 'xx.xxx.xxx.xxx',
    'port' => '8888',
    'keep-alive' => 30,
);
$clientSetting = array(
    'id' => 'id',
    'topic' => '/haco/traffic',
    'qos' => 1,
    'user' => 'foo',
    'password' => 'foo',
);
// Create MQTT Client
$client = new Mosquitto\Client($clientSetting['id']);

// Set handler
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
//$client->onLog('logger');

//Connect
$client->setCredentials($clientSetting["user"], $clientSetting["password"]);
$client->connect($brokerInfo["ip"], $brokerInfo["port"], $brokerInfo["keep-alive"]);

// Subscribe a topic
$client->subscribe($clientSetting["topic"], $clientSetting['qos']);

// Run
$client->loopForever();

function connect($rc, $message)
{
    // Success
    if ($rc == 0) {
        echo "Connected \n";
    } else {
        echo "Connection Error[code = {$rc}]: {$message} ";
    }
}
function subscribe(){
    echo "Subscribed to a topic\n";
}
function unsubscribe(){
    echo "Unsubscribed from a topic\n";
}
function message(){
    //usleep(1000);
}
function disconnect(){
    echo "Disconnected cleanly\n";
}
jhaoheng commented 7 years ago

Hello @cid2610

I try to connect broker and send message, If i send huge package of message, and memory usage more than small package. Maybe your package increase size by while loop.