title: Why the memory is not released after writing files on disk in C#
body: <p>I have a complicated algorithm which receives data from a socket connections, transformate the data and stores it as soon as possible on the HD..The data, because of that fact I don't want the processing to slow down is stored by using a different thread.
  The data storage algorithm resembles this structure. It essentially saves XML on the disk.</p>
  
  <pre><code>Begin Thread
   beginthread:
  
   XmlTextWriter xmltextWriter;
   Save Xml file 1
   xmltextWrite.close();
  
   XmlTextWriter xmltextWriter;
   Save Xml file 2
   xmltextWrite.close();
  
   goto beginthread:
  End Thread
  </code></pre>
  
  <p>It works correctly but if I get a look to the Task Manager , I could notice that the amount of memory consumed by my program encreases quickly over the time (500mb after 1 hour of work). This could be justified because of the fact that the thread is not so fast as the data which comes in and the .NET framework stores temporary all in memory for me. But what I didn't understand is why if the incoming socket connections will stop, even after a few minutes that the thread continues to work..the task manager continues to show 500Mb of memory..Why the memory is not relased ?! The XmlTextWriter object is a local variable and is closed every time.</p>
  
  <p>As requested.. This is part of the code</p>
  
  <pre><code>     beginthread:
          if (sleeptime &lt; 1000) sleeptime += 2;
  
          try
          {
  
              while (hashBeginConn.Count &gt; 0)
              {
                  sleeptime = 0;
  
                  int connToApply = hashBeginConn[0];
  
                  if (olddate.ToShortDateString() != ListsockConnections[connToApply].beginDate.ToShortDateString())
                  {
                      JoinDataFromTempFile(ListsockConnections[connToApply].beginDate.Date.Subtract(olddate.Date).Days, false, d);
                      olddate = ListsockConnections[connToApply].beginDate.Date;
                  }
  
                  if (tocreate)
                  {
                      // XML Serialization
                      XmlTextWriter xmltextWriter;
  
                      Encoding enc = null;
                      if (ListsockConnections[connToApply].ENCfromCode) enc = Encoding.GetEncoding(ListsockConnections[connToApply].codepage);
                      if (ListsockConnections[connToApply].ENCDefault) enc = Encoding.Default;
                      if (ListsockConnections[connToApply].ENCfromText) enc = Encoding.GetEncoding(ListsockConnections[connToApply].codename);
                      if (enc == null) { enc = null; }
  
                      // xmltextWriter = new XmlTextWriter(folderPath + "\\" + cacheFileName, enc);
                      xmltextWriter = new XmlTextWriter(DataPath + "\\_temp.xml", enc);
                      xmltextWriter.Formatting = Formatting.Indented;
  
                      // Start document
                      // xmltextWriter.WriteStartDocument();
                      xmltextWriter.WriteStartElement("ConnectionList");
                      xmltextWriter.WriteStartElement("connection");
  
                      xmltextWriter.WriteStartElement("ConnectionCounter");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].ConnectionCounter.ToString());
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteStartElement("IDConnection");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].IDConnection.ToString());
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteStartElement("Parsed");
                      xmltextWriter.WriteValue("false");
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteStartElement("connType");
                      xmltextWriter.WriteValue("TCP/IP");
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteStartElement("beginConn");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].beginDate.ToString());
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteStartElement("remoteAddressFamily");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].remoteAdressFamily);
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteStartElement("remoteIP");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].remoteIP);
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteStartElement("localIP");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].localIP);
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteStartElement("remoteport");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].remoteport.ToString());
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteStartElement("localport");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].localport.ToString());
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteStartElement("dataEncoding");
                      if (ListsockConnections[connToApply].codepage != 0 || ListsockConnections[connToApply].codename != "")
                      {
                          if (ListsockConnections[0].codepage != 0)
                          { xmltextWriter.WriteValue(ListsockConnections[connToApply].codepage.ToString()); }
                          else
                          { xmltextWriter.WriteValue(ListsockConnections[connToApply].codename.ToString()); }
                      }
                      else
                      { xmltextWriter.WriteValue("NONE"); }
                      xmltextWriter.WriteEndElement();
  
                      xmltextWriter.WriteEndElement();
                      xmltextWriter.WriteEndElement();
                      xmltextWriter.Flush();
                      xmltextWriter.Close();
  
                      tocreate = false;
                  }
                  else
                  {
                      FileInfo fi;
                      FileStream fstream;
  
                      //fi = new FileInfo(folderPath + "\\" + cacheFileName);
                      fi = new FileInfo(DataPath + "\\_temp.xml");
                      fstream = fi.OpenWrite();
  
                      XmlTextWriter xmltextWriter;
  
                      Encoding enc = null;
                      if (ListsockConnections[connToApply].ENCfromCode) enc = Encoding.GetEncoding(ListsockConnections[connToApply].codepage);
                      if (ListsockConnections[connToApply].ENCDefault) enc = Encoding.Default;
                      if (ListsockConnections[connToApply].ENCfromText) enc = Encoding.GetEncoding(ListsockConnections[connToApply].codename);
                      if (enc == null) { enc = null; }
  
                      xmltextWriter = new XmlTextWriter(fstream, enc);
  
                      xmltextWriter.Formatting = Formatting.Indented;
  
                      fstream.Position = fstream.Length - 17;
  
                      xmltextWriter.WriteRaw("  &lt;connection&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;ConnectionCounter&gt;");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].ConnectionCounter.ToString());
                      xmltextWriter.WriteRaw("&lt;/ConnectionCounter&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;IDConnection&gt;");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].IDConnection.ToString());
                      xmltextWriter.WriteRaw("&lt;/IDConnection&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;Parsed&gt;");
                      xmltextWriter.WriteValue("false");
                      xmltextWriter.WriteRaw("&lt;/Parsed&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;connType&gt;");
                      xmltextWriter.WriteValue("TCP/IP");
                      xmltextWriter.WriteRaw("&lt;/connType&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;beginConn&gt;");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].beginDate.ToString());
                      xmltextWriter.WriteRaw("&lt;/beginConn&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;remoteAddressFamily&gt;");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].remoteAdressFamily);
                      xmltextWriter.WriteRaw("&lt;/remoteAddressFamily&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;remoteIP&gt;");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].remoteIP);
                      xmltextWriter.WriteRaw("&lt;/remoteIP&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;localIP&gt;");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].localIP);
                      xmltextWriter.WriteRaw("&lt;/localIP&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;remotePort&gt;");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].remoteport.ToString());
                      xmltextWriter.WriteRaw("&lt;/remotePort&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;localport&gt;");
                      xmltextWriter.WriteValue(ListsockConnections[connToApply].localport.ToString());
                      xmltextWriter.WriteRaw("&lt;/localport&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("     &lt;dataEncoding&gt;");
                      if (ListsockConnections[connToApply].codepage != 0 || ListsockConnections[connToApply].codename != "")
                      {
                          if (ListsockConnections[connToApply].codepage != 0)
                          {
                              xmltextWriter.WriteValue(ListsockConnections[connToApply].codepage.ToString());
                          }
                          else
                          {
                              xmltextWriter.WriteValue(ListsockConnections[connToApply].codename.ToString());
                          }
                      }
                      else
                      {
                          xmltextWriter.WriteValue("NONE");
                      }
                      xmltextWriter.WriteRaw("&lt;/dataEncoding&gt;" + Environment.NewLine);
  
                      xmltextWriter.WriteRaw("  &lt;/connection&gt;" + Environment.NewLine);
                      xmltextWriter.WriteRaw("&lt;/ConnectionList&gt;");
  
                      xmltextWriter.Flush();
                      xmltextWriter.Close();
                      fstream.Close();
  
  
                      if (fi.Length &gt;= (maxFileTempSize * 1000000))
                      {
                          JoinDataFromTempFile(0, false, enc);
                      }
                  }
  
                  lock (lockThis)
                  {
                      hashBeginConn.RemoveAt(0);
                  }
  
              }
  </code></pre>