views:

143

answers:

4

Hi there,

I have been reading about the various memory management issues that .NET Asynchronous Sockets have.

There are but a handful of links, but spidering this one will get you them all: http://codebetter.com/blogs/gregyoung/archive/2007/06/18/async-sockets-and-buffer-management.aspx

Basically
when a socket is asynchronously sending/receiving many small byte[]'s
the send/receive byte[]'s are pinned in memory
leading to fragmentation.

SO for the purposes of creating a buffer manager : I have a managed buffer (byte[])

byte[] managedBuffer = new byte[1024];
// do stuff with managedBuffer;

how do I send this byte[] to the socket's asynchronous .BeginSend() method by reference?

// I don't want to pass the VALUE to the method, but a reference
// to managedBuffer;
System.Net.Sockets.Socket.BeginSend(managedBuffer...(other params));
+1  A: 

Arrays are always passed by reference, so you're already doing that. If you're using the socket asynchronously then you'll need to make sure you don't use managedBuffer while it's in progress.

Nik
+1  A: 

The only thing that BeginSend does with the byte data is sending it over to the opened Socket. After that the byte array would get disposed (at the end of the function, or when the class where the array is defined gets disposed) like any other byte array in .NET by the GC.

tomlog
+1  A: 

Arrays are always passed as a reference (like passing a pointer).

Jon B
+1  A: 

As indicated by Nik, you are already passing it by reference when you pass the byte[] to the method. However, there are benefits to using the ArraySegment method you found in the documentation; they are primarily to avoid memory fragmentation issues inherent in the pinning of the byte[] buffer during an async call.

See my answer to: http://stackoverflow.com/questions/869744/how-to-write-a-scalable-tcp-ip-based-server/908766#908766 for more details.

jerryjvl