title: .NET Remoting Singleton memory leak, TCP, Marshal by Reference
body: <p>I am using the simplest example of remoting that I could find, sharing an object between a windows service and a windows forms program (client), running on the same machine.</p>
  
  <p>The service instantiates the object like this:</p>
  
  <pre><code>serviceConfigRemote = new serviceConfigDataRemote();
  serverChannel = new TcpServerChannel(9090);
  ChannelServices.RegisterChannel(serverChannel, false);
  RemotingServices.Marshal(this.serviceConfigRemote, "ServiceConfigData");
  </code></pre>
  
  <p>The client establishes a connection like this:</p>
  
  <pre><code>TcpClientChannel channel = new TcpClientChannel();
  ChannelServices.RegisterChannel(channel, false);
  configData = (serviceConfigDataRemote)Activator.GetObject(typeof(serviceConfigDataRemote), "tcp://localhost:9090/ServiceConfigData");
  </code></pre>
  
  <p>The idea is for the service to be able to make changes to some of the parameters of the object, for the client to be able to read those changes.  </p>
  
  <p>The object itself is:</p>
  
  <pre><code>public sealed class serviceConfigDataRemote : MarshalByRefObject
  {
      private bool myConnectedFlag;
      private bool mySendingFlag;
      private bool myUpdateFlag;
      private string myClientConfiguration;
  
      static readonly serviceConfigDataRemote instance = new serviceConfigDataRemote();
  
      static serviceConfigDataRemote()
      {
      }
  
      public serviceConfigDataRemote()
      {
          myConnectedFlag = false;
          mySendingFlag = false;
          myUpdateFlag = false;
          myClientConfiguration = "";
      }
  
      public static serviceConfigDataRemote Instance
      {
          get
          {
              return instance;
          }
      }
  
      public override object InitializeLifetimeService()
      {
          return (null);
      }
  
  
      public bool Connected
      {
          get { return myConnectedFlag; }
          set { myConnectedFlag = value; }
      }
  
      public bool Sending
      {
          get { return mySendingFlag; }
          set { mySendingFlag = value; }
      }
  
      public bool CheckForUpdates
      {
          get{return myUpdateFlag;}
          set { myUpdateFlag = value; }
      }
  
      public string ClientConfiguration
      {
          get { return myClientConfiguration; }
          set { myClientConfiguration = value; }
      }
  }
  </code></pre>
  
  <p>While the service is running by itself, the Mem Usage in Task Manager stays constant, even though the service is continually updating the object with status information.  When the client is started, both begin to increase in Mem Usage, and never go down.</p>
  
  <p>This is the problem that I referred to in <a href="https://stackoverflow.com/questions/1840090/how-do-i-find-a-net-remoting-memory-leak-on-select-machines">My Previous Question</a> about finding memory leaks.</p>
  
  <p>It is appearing differently on different machines, some show no memory increases, but the machines that do will reliably reproduce this problem.  Running .NET Memory Profiler shows that on the service, there is an ever increasing number of "New instances", with only one or two "Removed" in the tab Types/Resources where Namespace/System is Kernel and Name/Resource is HeapMemory.  I'm still trying to learn how to use the Memory Profiler, so I apologize if this is the wrong information, and tip on where else I should be looking would also be appreciated.</p>
  
  <p>This object is instantiated once, with just a couple of parameters to read and write, no file io, no allocating of memory that I can see, and yet my memory usage only appears to go up the moment I start a connection from the client to that object and read its values.  Any and all input would be appreciated, as I would like to avoid pulling this code and replacing it with named pipes or similar, but I'm quickly approaching that point as my only option.</p>