mhowlett / NNanomsg

.NET binding for nanomsg
MIT License
179 stars 52 forks source link

Memory leaks in subscription socket #21

Open Sergey-Terekhin opened 9 years ago

Sergey-Terekhin commented 9 years ago

I recieve memory leaks (few kilobytes per message recieved) when using pub-sub pattern at the subscriber side. It looks independent from recieved message size My code is (simplified):

while (true)
{
    var eventData = _socket.Receive();
    if (eventData == null)
    {
           continue;
    }
    HandleData(eventData);
}

I use version from NuGet

Sergey-Terekhin commented 9 years ago

_socket is SubscribeSocket

mhowlett commented 9 years ago

I expect this is an issue with nanomsg itself because NNanomsg is all managed code. We are using the last nanomsg release (v0.5). I believe there are substantial fixes after this in master, though I also believe it currently doesn't compile under Windows. You could try compiling a more recent version of nanomsg and see if that fixes the issue.

mhowlett commented 9 years ago

But actually, how are you measuring that? if you're just watching the process size grow, it'll just be that the GC isn't freeing memory allocated in Receive method very quickly - it will eventually.

Sergey-Terekhin commented 9 years ago

I use memory profiler (dotMemory) and see that manage memory is freed but unmanaged slowly grows. I'll try to build latest release of nanomsg and check leaks there

kwpatrick commented 9 years ago

Sergey - Is this specific to a transport?

kwpatrick commented 9 years ago

I found an issue with the handling of zero-byte messages and subscriptions which could produce a native leak. Seems unlikely, but any chance that this is what you're seeing Sergey?

If not, please include some more details about your platform and scenario. I couldn't reproduce this for non-zero length messages on any of the transports.

Sergey-Terekhin commented 9 years ago

I've found memory leak with non-zero messages (I've not tested zero-byte messages). And it is seems to be independent of message size - something like unmanaged descriptor leak. I see memory leaks on Windows 7 Pro x64, but where are no any leaks on Windows 8.1 Pro x64. I'll check this more carefully I use tcp transport for pub-sub sockets and localhost connection

sfmskywalker commented 8 years ago

Any updates on this issue?

mhowlett commented 8 years ago

There is a new version of nanomsg out (0.6) that I haven't added binaries for because I don't currently have a windows machine ... This version evidently fixes a number of issues so it might be worth trying that. The new binaries should be able to be just dropped in without changes to NNanomsg (compile instructions are in the NNanomsg readme). I've been thinking it might also be worth trying to wrap mangos - the go implementation which is evidently much more stable at this point. I haven't looked into it but I believe it to be possible.