zeromq / libzmq

ZeroMQ core engine in C++, implements ZMTP/3.1
https://www.zeromq.org
Mozilla Public License 2.0
9.62k stars 2.35k forks source link

MeMemory leak zmq msg_t initsize ? #4400

Open constantmanish opened 2 years ago

constantmanish commented 2 years ago

Please use this template for reporting suspected bugs or requests for help.

Issue description

We are using zmq inproc socket to transmit messages between threads. we are sending multipart messages send() method with the necessary flag. We are seeing this behavior of large memory consumption when large amount of messages are being transmitted using this socket. I looked into the page-fault graph and can see that most of the page-fault is created by send() part of code, I tried to trace native allocation using jemalloc and the output obtained also suggest large allocation during SocketSend. From jemalloc output I can see that leak might be associated with zmq msg_t init_size() function. I have attached graph of what I have described above. . Can someone help me how to tackle the issue. It could have been the case that message was not closed properly, but I looked into jni code and can see it's getting closed for every case. Code snippet of Java_org_zeromq_SocketSend `JNIEXPORT jboolean JNICALL Java_org_zeromq_ZMQ_00024Socket_send (JNIEnv env, jobject obj, jbyteArray msg, jint offset, jint length, jint flags) { void s = get_socket (env, obj);

if (length < 0) {
    raise_exception(env, EINVAL);
    return JNI_FALSE;
}

zmq_msg_t message;
int rc = zmq_msg_init_size (&message, length);
int err = zmq_errno();
if (rc != 0) {
    raise_exception (env, err);
    return JNI_FALSE;
}

void* pd = zmq_msg_data (&message);
env->GetByteArrayRegion(msg, offset, length, (jbyte*) pd);

if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0)

rc = zmq_sendmsg (s, &message, flags);

else

rc = zmq_send (s, &message, flags);

endif

err = zmq_errno();

if (rc < 0 && err == EAGAIN) {
    rc = zmq_msg_close (&message);
    err = zmq_errno();
    if (rc != 0) {
        raise_exception (env, err);
        return JNI_FALSE;
    }
    return JNI_FALSE;
}

if (rc < 0) {
    raise_exception (env, err);
    rc = zmq_msg_close (&message);
    err = zmq_errno();
    if (rc != 0) {
        raise_exception (env, err);
        return JNI_FALSE;
    }
    return JNI_FALSE;
}

rc = zmq_msg_close (&message);
err = zmq_errno();
if (rc != 0) {
    raise_exception (env, err);
    return JNI_FALSE;
}

return JNI_TRUE;

} `

Environment

constantmanish commented 2 years ago

@bluca can you help me regarding this ??