在执行循环的时候程序占的内存越来越大,直到内存溢出,如何能释放内存

不想再业余 2017-05-25 03:26:07
public bool CreateGISIndexesByKey(out string errMsg)
{
JD_TotalEntities entity = new JD_TotalEntities();
List<datum> datas = entity.data.ToList();
foreach (datum data in datas)
{
string folderId = data.DataFolderId;
string folders = data.DataFolders;
List<string> Folderlevel = new List<string>();
Folderlevel = folders.Split(',').ToList();
string folderPath = null;
if (Folderlevel.Count < 5)
{
folderPath = String.Format("{0}\\{1}", GlobalSetting.LucencePath3, Folderlevel[0]);
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
}
else
{
folderPath = String.Format("{0}\\{1}", GlobalSetting.LucencePath3, Folderlevel[Folderlevel.Count - 4]);
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
}
List<dataindex> DataIndexIds = entity.dataindexes.Where(di=>di.DataFolderId==data.DataFolderId).ToList();
foreach (dataindex DataIndexId in DataIndexIds)
{
var entityIndexValues = entity.entityindexvalues.Where(e => e.DataIndexId == DataIndexId.DataIndexId);
int count = entityIndexValues.Count();
int index = count / 10000;
if (count % 10000 != 0)
index++;
for (int i = 0; i < index; i++)
{
List<entityindexvalue> indexValues = entityIndexValues.OrderBy(o => o.DataEntityId).Skip(i * 10000).Take(10000).ToList();
List<LucenceDataGISEntity> GISEntitys = new List<LucenceDataGISEntity>();
foreach (entityindexvalue indexValue in indexValues)
{
LucenceDataGISEntity GISEntity = new LucenceDataGISEntity();
GISEntity.Key = indexValue.DataIndexId;
GISEntity.Value = indexValue.EntityIndexValue1 == null ? "" : indexValue.EntityIndexValue1.ToString();
GISEntity.EntityId = indexValue.DataEntityId;
dataentity dataentity = entity.dataentities.FirstOrDefault(o => o.DataEntityId == indexValue.DataEntityId);
GISEntity.IsGISData = (dataentity.DataLong != null && dataentity.DataLat != null) ? "1" : "0";
GISEntity.EntityLong = dataentity.DataLong == null ? "-1" : dataentity.DataLong.ToString();
GISEntity.EntityLat = dataentity.DataLat == null ? "-1" : dataentity.DataLat.ToString();
GISEntity.Folders = folders;

GISEntitys.Add(GISEntity);
}
IndexManager.CreateGISIndexsByKey(folderPath, folderId, GISEntitys, out errMsg);
}

}
}
errMsg = null;
return true;
}
...全文
2201 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
不想再业余 2017-05-26
  • 打赏
  • 举报
回复
引用 9 楼 daixf_csdn 的回复:
会不会是 IndexManager.CreateGISIndexsByKey造成的,你把这句话屏蔽掉,空跑,看内存变化情况怎么样。
不是这个的原因
圣殿骑士18 2017-05-25
  • 打赏
  • 举报
回复
会不会是 IndexManager.CreateGISIndexsByKey造成的,你把这句话屏蔽掉,空跑,看内存变化情况怎么样。
ilikeff8 2017-05-25
  • 打赏
  • 举报
回复
如果内部代码有非托管代码,要注意手动释放
xuzuning 2017-05-25
  • 打赏
  • 举报
回复
怎么没看到给 entity 赋值?
xuggzu 2017-05-25
  • 打赏
  • 举报
回复
最大的消耗在IndexManager.CreateGISIndexsByKey()

建议该函数一次处理一部分,这样new的list会小很多。
不想再业余 2017-05-25
  • 打赏
  • 举报
回复
引用 3 楼 Libby1984 的回复:
创建了太多的List,而且list的元素都是类,如果这些类里面有无法自动回收的资源的话也需要你手动的释放
那该如何释放呢
不想再业余 2017-05-25
  • 打赏
  • 举报
回复
引用 2 楼 duanzi_peng 的回复:
每次循环结束都将对应的List进行clear呢
clear好像并不能释放掉
  • 打赏
  • 举报
回复
创建了太多的List,而且list的元素都是类,如果这些类里面有无法自动回收的资源的话也需要你手动的释放
exception92 2017-05-25
  • 打赏
  • 举报
回复
每次循环结束都将对应的List进行clear呢
不想再业余 2017-05-25
  • 打赏
  • 举报
回复
这里3层的foreach循环 第一层大概100多次,第二层20-30次,第三层最大10000次 for循环只有几次,这个IndexManager.CreateGISIndexsByKey()建了些索引文件,建完之后都close掉了

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧