Mosquitto-PHP icon indicating copy to clipboard operation
Mosquitto-PHP copied to clipboard

Memory garbage collection

Open toan-leminh opened this issue 10 years ago • 3 comments

I do stress test below:

  • PHP Client subsriber a topic
  • Use MQTT fx ( I use Mac) with script to Publish message continuously (500ms/message) So PHP Client's memory increases gradually, although I don't do anything in callback. Is this PHP Client's problem or PHP garbage collection (I use PHP 5.5) ?

toan-leminh avatar Oct 20 '15 09:10 toan-leminh

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();

mgdm avatar Oct 20 '15 10:10 mgdm

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";
}

toan-leminh avatar Oct 21 '15 03:10 toan-leminh

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.

jhaoheng avatar Apr 02 '17 01:04 jhaoheng