2011-09-15 31 views
3

我得到了一個WCF服務,它有一個簡單的方法稱爲GetLineBusses。WCF私人內存使用增加查詢查詢

public MobileResponse GetLineBusses(MobileRequest Request) 
    { 
     MobileResponse response = new MobileResponse(); 
     using (var entities = new NerdeBuOtobusEntities()) 
     { 


      Line line = null; 
      List<Point> points = new List<Point>(); 
      City city = entities.Cities.SingleOrDefault(t => t.ID == Request.CityID); 
      try 
      { 
       line = entities.Lines.SingleOrDefault(t => t.ID == Request.LineID); 
       //if (line != null) 
       //{ 
       // points = entities.Points.ToList().Where(t => t.LineID == Request.LineID && Request.WithPoints == true).ToList(); 
       //} 
      } 
      catch (System.Exception ex) 
      { 

      } 
      FetcherManager fetcherManager = new FetcherManager(city); 
      List<Bus> busses = fetcherManager.GetLineBusses(line); 

      List<BusDTO> busDtos = new List<BusDTO>(); 

      foreach (Bus bus in busses) 
      { 
       BusDTO aBus = new BusDTO(bus); 
       busDtos.Add(aBus); 
      } 
      response.Points = points.Select(t => new PointDTO(t)).ToList(); 
      response.Busses = busDtos; 
     } 

     return response; 
    } 

我觀察到的是,當我發佈上述方法,每個查詢方法增加了我的IIS工作進程RAM使用多達160,000 KB。爲了找出問題,我註釋掉

City city = entities.Cities.SingleOrDefault(t => t.ID == Request.CityID); 

line = entities.Lines.SingleOrDefault(t => t.ID == Request.LineID); 

現在的方法公羊使用最小化到20,000。我認爲問題是因爲實體框架特別是LINQ。順便說一句,我已經嘗試了靜態查詢,以減少內存使用,但它根本無法工作。我怎麼解決這個問題?我想通過使用LINQ,以儘量減少RAM使用...

問候

末爾

+1

什麼是FetcherManager?如果你調試了,你會發現究竟哪行內存分配被激發?你也是危險的hidng一個例外,沒有任何日誌記錄或拋出......這些問題不解決你的問題,這就是爲什麼是一個評論:) –

+0

fetcher管理器是一個連接到一些網站,使得獲取和解析操作的管理器。但是,問題不在於收件人管理器,因爲當我測試兩種情況下的方法時,我根本沒有刪除收件人管理器。 – kkocabiyik

+0

這可能是ID沒有正確索引? – Junaid

回答

0

下面一行是在entities.Points上調用ToList()。這導致整個Points表被加載到DataContext中,然後應用Where()子句。

points = entities.Points.ToList().Where(t => t.LineID == Request.LineID && Request.WithPoints == true).ToList(); 

嘗試在entities.Points後刪除ToList()。例如,

points = entities.Points.Where(t => t.LineID == Request.LineID && Request.WithPoints == true).ToList(); 
+0

這條線也不起作用,它也被註釋掉了。 – kkocabiyik

0

看起來像實體框架負載從每一個方法調用表中的所有數據。 檢查實體框架的版本。 純性能已知4.0以前的版本。 也嘗試指定一些選擇查詢以減少數據量。

+0

我也試過急於加載,但它沒有解決任何問題。我正在使用EF 4.1 ... – kkocabiyik