Search code examples
c#wcf-ria-servicessilverlight-5.0

how to release memory for wcf ria service call?


I have following code for a silverlight app wit wcf ria service in a static method:

public static void mymethod(){
DomainContext context = new DomainContext();
var q = context.GetMyEntitiesQuery().Where(x => x.Name == name );
context.Load<MyEntity>(q, LoadBehavior.RefreshCurrent,
  (p) =>
  {
  if (!p.HasError)
    {
     //......     
    }

  }, null);
}

It is working fine. but every time when I call this method, the memory will be increased about 3M for browser process and it's never released.

How to release the memory for this case?


Solution

  • You should as few instances of DomainContext as possible. Perhaps pass the context from the caller

    Thing.mymethod(context);
    

    or keep a static instance

    public static class Thing
    {
        DomainContext _Context = new DomainContext();
    
        public static void mymethod()
        {
            var q = _context.GetMyEntitiesQuery().Where(x => x.Name == name );
            ...
        }
    }
    

    Every instance you create is probably sticking around and creating that memory leak.