c# - Socket receive does not release the buffer (byte array) -
i'm developing service listens messages in tcp protocol, , send them different destinations.
so defined socket , stays open time , gets messages . size of protocol static , messages arrive few @ time.
the loop runs long values received , cuts matching size of messages.
the service works well- messages received needed memory gets , up. implementation:
public void waitingforclient() { while (true) { // accept block until connects m_socket = m_tcplsn.acceptsocket(); threadpool.queueuserworkitem(new waitcallback(readsocket)); } } //read data socket public void readsocket(object obj) { socket s = m_socket; if (s.connected) { byte[] receive = new byte[message_length]; try { // receive block until data coming // ret 0 or exception happen when socket connection broken int ret = s.receive(receive, receive.length, 0); while (ret > 0) { onmessagerecieved(new recievedmessage(receive)); **receive = new byte[message_length];** ret = s.receive(receive, receive.length, socketflags.none); } }
i saw in net memory profiler buffer(a byte array defind in loop), never gets freed , therefore there problem of memory leak. in every sending of message instanse of byte array created & nothing freed.
i tried write loop initializes array in nulls way:
try { // receive block until data coming // ret 0 or exception happen when socket connection broken int ret = s.receive(receive, receive.length, 0); while (ret > 0) { onmessagerecieved(new recievedmessage(receive)); (int = 0; < message_length; i++) { receive[i] = 00; } ret = s.receive(receive, receive.length, socketflags.none); }
but part of messages sent nulls instead of right values(messages joined together).
how can solve it?
Comments
Post a Comment