title: How to clear system memory after BeginReceive() and EndReceive() function?
body: <p>I am doing a project where I am connecting to an USSD server using socket. This project is a scheduler which runs real time and 24/hrs. </p>
  
  <p>Below is my code for <code>BeginReceive()</code> and <code>EndReceive()</code></p>
  
  <pre><code>Public Sub Receive(ByVal client As Socket)
  
      Dim state As New StateObject
      state.workSocket = client
  
      Try
  
          If m_clientSocket.Connected Then
  
               client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
           Else
               Connect()
           End If
  
      Catch se As SocketException
  
          logstr = se.Message
          write_log(logstr, "Receive", "Receive")
  
      End Try
  
  End Sub
  
  Public Sub ReceiveCallback(ByVal ar As IAsyncResult)
  
      Dim state       As StateObject = CType(ar.AsyncState, StateObject)
      Dim client      As Socket      = state.workSocket
      Dim receiveData As String      = ""
      Dim sendData    As String      = ""
  
      Dim bytesRead   As Integer     = client.EndReceive(ar)
  
      If bytesRead &gt; 0 Then
  
          state.sb.Append(System.Text.Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
  
          client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)      
  
          receiveData = state.sb.ToString()
  
          Me.lsBulk.Items.Insert(0, receiveData)
          sendData = getReply(receiveData)
          write_log(Now.ToString &amp; " : " &amp; receiveData &amp; vbCrLf, "bytesRead", "bytesRead")
  
          Try
  
              Dim objData As [Object] = sendData
              Dim byData As Byte() = System.Text.Encoding.ASCII.GetBytes(objData.ToString())
              If m_clientSocket IsNot Nothing Then
                  m_clientSocket.Send(byData)
              End If
  
              write_log(Now.ToString &amp; " : " &amp; sendData &amp; vbCrLf, "sendData", "sendData")
  
          Catch se As SocketException
  
              logstr = se.Message
              write_log(logstr, "WaitForData", "waitfordata")
  
          End Try
  
      Else
  
          receiveData = state.sb.ToString()
  
          If receiveData &lt;&gt; "" Then
  
              write_log(Now.ToString &amp; " : " &amp; receiveData &amp; vbCrLf, "receiveData2", "receiveData2")
  
          End If
  
      End If
  
  End Sub 
  </code></pre>
  
  <p>The problem is this program is taking so much of my memory. After running for the first time it takes 3.5GB then after an hour it takes 4.5GB and so on until or unless we restart it. It is becoming a big trouble for the server as it is allocating a lot of space in the memory.</p>
  
  <p>I can't understand at which point I have to clear the buffer to release the system memory. The data in the buffer is not necessary to hold as every time it is writing to database.</p>
  
  <p>I would like to mention that the buffer size here is 10024 every time.</p>