WCF private memory use increase query by query
I got a WCF service that has simple method called GetLineBusses.
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;
}
What I observed is that when I publish the method above, each query to method increases my IIS worker process ram use up to 160,000 kb. In order to find out the problem, I commented out
City city = entities.Cities.SingleOrDefault(t => t.ID == Request.CityID);
and
line = entities.Lines.SingleOrDefault(t => t.ID == Request.LineID);
Now that the ram use for method is minimized to 20,000. I think the problem is because of entity framework especially LINQ. By the way, I have tried static queries in order to decrease the memory use however It did not work at all. How can I solve this problem? I want to minimize the ram use by using LINQ...
Regards
Kemal
The below line is calling ToList() on entities.Points. This results in the entire Points table being loaded into the DataContext, and then the Where() clause is applied.
points = entities.Points.ToList().Where(t => t.LineID == Request.LineID && Request.WithPoints == true).ToList();
Try removing the ToList() after entities.Points. For example,
points = entities.Points.Where(t => t.LineID == Request.LineID && Request.WithPoints == true).ToList();
Looks like Entity Framework load all data from tables on every method call. Check version of Entity Framework. Versions older than 4.0 are known by pure performance. Also try to specify some select query to reduce amount of data.
精彩评论