WCF 私有内存使用量逐个查询增加
我有一个 WCF 服务,它有一个名为 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;
}
我观察到,当我发布上述方法时,对方法的每个查询都会将我的 IIS 工作进程 RAM 使用量增加到 160,000 kb。为了找出问题所在,我注释掉了
City city = entities.Cities.SingleOrDefault(t => t.ID == Request.CityID);
Now
line = entities.Lines.SingleOrDefault(t => t.ID == Request.LineID);
that the ram use for 方法最小化到20,000。我认为问题是由于实体框架尤其是 LINQ 造成的。顺便说一句,我尝试过静态查询以减少内存使用,但它根本不起作用。我该如何解决这个问题?我想通过使用 LINQ 最大限度地减少内存使用...
问候
Kemal
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的行正在对Entity.Points 调用ToList()。这会导致整个 Points 表被加载到 DataContext 中,然后应用Where() 子句。
尝试删除Entity.Points之后的ToList()。例如,
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.
Try removing the ToList() after entities.Points. For example,
看起来实体框架在每次方法调用时加载表中的所有数据。
检查实体框架的版本。
4.0 之前的版本以纯粹的性能而闻名。
还可以尝试指定一些选择查询以减少数据量。
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.