1382

socket buffer size: pros and cons of bigger vs smaller

Question:

I've never really worked with COM sockets before, and now have some code that is listening to a rather steady stream of data (500Kb/s - 2000Kb/s).

I've experimented with different sizes, but am not really sure what I'm conceptually doing.

byte[] m_SocketBuffer = new byte[4048]; //vs byte[] m_SocketBuffer = new byte[8096];

The socket I'm using is System.Net.Sockets.Socket, and this is my constructor:

new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

My questions are:

<ol><li>Is there a general trade-off for large vs. small buffers?</li> <li>How does one go about sizing the buffer? What should you use as a gauge?</li> </ol>

I'm retrieving the data like this:

string socketData = Encoding.ASCII.GetString(m_SocketBuffer, 0, iReceivedBytes) while (sData.Length > 0) { //do stuff } <ol start="3"><li>Does my reading event happen when the buffer is full? Like whenever the socket buffer hits the threshold, that's when I can read from it?</li> </ol>

Answer1:

Short version:

<ul><li>Optimal buffer size is dependent on a lot of factors, including the underlying network transport and how your own code handles the I/O.</li> <li>10's of K is probably about right for a high-volume server moving a lot of data. But you can use much smaller if you know that remote endpoints won't ever be sending you a lot of data all at once.</li> <li>Buffers are pinned for the duration of the I/O operation, which can cause or exacerbate heap fragmentation. For a really high-volume server, it might make sense to allocate very large buffers (larger than 85,000 bytes) so that the buffer is allocated from the large-object heap (which either has no fragmentation issues, or is in a perpetual state of fragmentation, depending on how you look at it :) ), and then use just a portion of each large buffer for any given I/O operation.</li> </ul>

Re: your specific questions:

<blockquote> <ol><li>Is there a general trade-off for large vs. small buffers?</li> </ol></blockquote>

Probably the most obvious is the usual: a buffer larger than you will ever actually need is just wasting space.

Making buffers too small forces more I/O operations, possibly forcing more thread context switches (depending on how you are doing I/O), and for sure increasing the number of program statements that have to be executed.

There are other trade-offs of course, but to go into each and every one of them would be far too broad a discussion for this forum.

<blockquote> <ol start="2"><li>How does one go about sizing the buffer? What should you use as a gauge?</li> </ol></blockquote>

I'd start with a size that seems "reasonable", and then experiment from there. Adjust the buffer size in various load testing scenarios, increasing and decreasing, to see what if any effect there is on performance.

<blockquote> <ol start="3"><li>Does my reading event happen when the buffer is full? Like whenever the socket buffer hits the threshold, that's when I can read from it?</li> </ol></blockquote>

When you read from the socket, the network layer will put as much data into your buffer as it can. If there is more data available than will fit, it fills the buffer. If there is less data available than will fit, the operation will complete without filling the buffer (but always when at least one byte has been placed into the buffer…the only time a read operation completes with zero-length is when the connection is being shut down)

Recommend

  • Receive GET request through sockets
  • javascript post
  • A basic Monoid definition gives “No instance for (Semigroup MyMonoid) arising from the superclasses
  • How to parse XML with repeated XML tags into a POJO using Jackson?
  • Return ImageView instance when i click on the item GridView
  • What Causes Instruction Replay Overhead in CUDA
  • Possible to “watch” both HAML and SASS at the same time?
  • How to make R's read_csv2() recognise the text characters properly
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • Installed module is empty
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • d3 v4 drag and drop with TypeScript
  • print() is showing quotation marks in results
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • JQuery Internet Explorer and ajaxstop
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Android fill_parent issue
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • CSS Linear-gradient formatting issue accross different browsers
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Get object from AWS S3 as a stream
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • retrieve vertices with no linked edge in arangodb
  • How to disable jQuery.jplayer autoplay?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • -fvisibility=hidden not passed by compiler for Debug builds
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?