Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redisclient publish api memory leak #84

Open
zhanmail opened this issue Feb 10, 2023 · 0 comments
Open

redisclient publish api memory leak #84

zhanmail opened this issue Feb 10, 2023 · 0 comments

Comments

@zhanmail
Copy link

zhanmail commented Feb 10, 2023

@nekipelov @elnull use redisclient pubsub to publish message, but when i publish message 5000000, one message 290 Bytes, it will occur 2G memory leak above. top command show 2G memory that is not free, but it is no memory leak when i use gperftools or valgrind to check memory leak. code is below :

#include
#include
#include
#include <boost/asio/ip/address.hpp>
#include <boost/format.hpp>
#include <boost/asio/deadline_timer.hpp>

#include <redisclient/redisasyncclient.h>

#include <gperftools/heap-profiler.h>

static const std::string channelName = "test_msg";
static const boost::posix_time::seconds timeout(1);
static std::atomic_int unique_send(0);
static std::atomic_int unique_cb_succ(0);
static std::atomic_int unique_cb_fail(0);
static std::atomic_int unique_pub_err(0);

class Client
{
public:
Client(boost::asio::io_service &ioService)
: ioService(ioService), publisher(ioService)
{
boost::asio::ip::address address = boost::asio::ip::address::from_string("127.0.0.1");
const unsigned short port = 6379;
boost::asio::ip::tcp::endpoint endpoint(address, port);

    publisher.installErrorHandler(std::bind(&Client::onPublishError, this, std::placeholders::_1));

    publisher.connect(endpoint, [&](boost::system::error_code ec)
    {
        if(ec)
        {
            std::cerr << "publisher Can't connect to redis: " << ec.message() << std::endl;
            return;
        }

        //publishHandler(std::atoi(argv[1]), std::ref(publisher));
        publishHandler(5000000, std::ref(publisher));
    });
}

void onPublishError(const std::string& error) {
    unique_pub_err++;
    std::cout << "publish error : " << error << ", count : " << unique_pub_err << std::endl;
}

void publishHandler(const int count, redisclient::RedisAsyncClient &publisher)
{
    std::string msg = "{\"data\":{\"loc\":{\"1\":[\"{\\\"pos\\\": [17429, 0, 124300], \\\"yawDeg\\\": 0, \\\"state\\\": 1, \\\"speed\\\": 1500, \\\"seatName\\\": \\\"\\\", \\\"actionType\\\": -1, \\\"camera\\\": 0, \\\"mic\\\": 0, \\\"imGid\\\": 0, \\\"imColor\\\": \\\"\\\", \\\"ts\\\": 1673856701650}\",1673856701650]}},\"spid\":1,\"type\":102,\"users\":1,\"version\":1}";
    redisclient::RedisBuffer message(msg);
    
    for (int k = 0; k < count; k++) {
        unique_send++;
        //std::cout << "publish count : " << unique_send << std::endl;
        publisher.publish(channelName, message, [&](redisclient::RedisValue v) {
            int code = v.isOk() ? 0 : -1;
            if (code != 0) {
                unique_cb_fail++;
                std::cout << "publishedHandler publish failed count : " << unique_cb_fail << std::endl;
            } else {
                unique_cb_succ++;

                if (unique_cb_succ == 5000000) {
                    std::cout << "publishedHandler publish success count : " << unique_cb_succ << std::endl;
                    ioService.stop();
                    sleep(60*2);
                }
            }
        });
    }
}

private:
boost::asio::io_service &ioService;
redisclient::RedisAsyncClient publisher;
};

int main(int argc, char ** argv)
{
if (argc < 1) {
std::cout << "usage error, please use like below : " << std::endl;
std::cout << "async_pubsub3 loop_count" << std::endl;
return -1;
}

boost::asio::io_service ioService;
Client client(ioService);
ioService.run();

return 0;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant