title: Sql Server 2008 R2: AppDomain is marked for unload due to memory pressure
body: <p>I am running Sql Server 2008 R2 Express with a database used by a service. The database is attached via AttachDBFileName option at service startup. The service runs constantly and restarts only after automatic updates.</p>
  
  <p>Sometimes, this pattern appears in Sql Server Log:</p>
  
  <pre><code>10/09/2013 11:18:05,spid18s,Unknown,AppDomain 6 (..MDF.dbo[runtime].5) unloaded.
  10/09/2013 11:18:05,spid1s,Unknown,AppDomain 6 (...MDF.dbo[runtime].5) is marked for unload due to memory pressure.
  10/09/2013 11:16:16,spid53,Unknown,AppDomain 6 (....MDF.dbo[runtime].5) created.
  10/09/2013 11:16:00,spid28s,Unknown,AppDomain 5 (....MDF.dbo[runtime].4) unloaded.
  10/09/2013 11:16:00,spid1s,Unknown,AppDomain 5 (....MDF.dbo[runtime].4) is marked for unload due to memory pressure.
  10/09/2013 11:15:41,spid53,Unknown,AppDomain 5 (...MDF.dbo[runtime].4) created.
  10/09/2013 11:14:20,spid24s,Unknown,AppDomain 4 (...MDF.dbo[runtime].3) unloaded.
  10/09/2013 11:14:20,spid1s,Unknown,AppDomain 4 (...MDF.dbo[runtime].3) is marked for unload due to memory pressure.
  </code></pre>
  
  <p>This causes performance problems, because every time the AppDomain is restarted, buffers are dropped and the database is unresponsive for a few seconds.</p>
  
  <p>According to <a href="http://support.microsoft.com/kb/917271?wa=wsignin1.0" rel="nofollow">http://support.microsoft.com/kb/917271?wa=wsignin1.0</a>, it might be due to memory leaks or other errors in CLR modules (even though I suppose that should be fixed in Sql Server 2008 R2). There is one CLR module installed, with the following code:</p>
  
  <pre><code>[Serializable]
  [Microsoft.SqlServer.Server.SqlUserDefinedAggregate(Format.UserDefined, 
      MaxByteSize=8000)]
  public class Concatenate : IBinarySerialize
  {
      /// &lt;summary&gt;
      /// The variable that holds the intermediate result of the concatenation
      /// &lt;/summary&gt;
      private StringBuilder intermediateResult;
  
      public void Init()
      {
          // Put your code here
          intermediateResult = new StringBuilder();
      }
  
      public void Accumulate(SqlString Value)
      {
          if (Value.IsNull)
              return;
  
          intermediateResult.Append(Value.Value).Append(", ");
      }
  
      public void Merge(Concatenate Group)
      {
          intermediateResult.Append(Group.intermediateResult);
      }
  
      public SqlString Terminate()
      {
          string output = string.Empty;
          //delete the trailing comma, if any
          if (intermediateResult != null &amp;&amp; intermediateResult.Length &gt; 0)
              output = intermediateResult.ToString(0, intermediateResult.Length - 2);
  
          return new SqlString(output);
      }
  
      #region IBinarySerialize Members
  
      public void Read(System.IO.BinaryReader r)
      {
          if (r == null) throw new ArgumentNullException("r");
          intermediateResult = new StringBuilder(r.ReadString());
      }
  
      public void Write(System.IO.BinaryWriter w)
      {
          if (w == null) throw new ArgumentNullException("w");
          w.Write(intermediateResult.ToString());
      }
  
      #endregion
  }
  </code></pre>
  
  <p>Also, MS Exchange is running on the server, using up 90% of free memory for MDB store. However I saw this errors in the past on other servers with heaps of free memory, so I suppose the memory pressure is not the real cause of the problem.</p>
  
  <p>Any ideas what could cause this problem?</p>