拼接xml时出现内存溢出解决办法

时间:2022-01-24 09:56:32
 /// designed by Alex
/// 解决内存溢出问题,不可使用迭代器和foreach
/// 循环遍历List, 别生成xml,一边释放
/// 反向遍历,相当于出栈,避免索引顺序乱的问题
/// 反转List.
if (zrate.RateList != null && zrate.RateList.Count != )
{
log.Debug("反转list");
zrate.RateList.Reverse();
log.Debug("循环遍历Zrate List 生成xml");
int policySize = zrate.RateList.Count;
for (int index = policySize - ; index > -; index--)
{
SplitPolicy item = zrate.RateList[index];
string strChangePnr = item.IsChangePnr ? "" : "";
string strCabin = item.Cabin;
try
{
strCabin = strCabin.Replace("/", "");
}
catch { }
decimal c = item.Rate + item.RuleRate;
string d = item.IsAutoTicket ? "" : "";
string strWeekNum = item.WeekNum;
if (strWeekNum != "")
{
strWeekNum = strWeekNum.Replace("/", ",");
}
string ZtFlightNumber = "";
try
{
ZtFlightNumber = item.ApplyFlightNumber.Replace('/', ',');
}
catch { }
string ZtNoFlightNumber = "";
try
{
ZtNoFlightNumber = item.NotApplyFlightNumber.Replace('/', ',');
}
catch { } string WorkTimeWeekDay = "";
string RefundWorkTimeWeekDay = "";
//工作时间
for (int i = ; i < ; i++)
{
WorkTimeWeekDay = WorkTimeWeekDay + item.WorkTimeWeekDay + ",";
RefundWorkTimeWeekDay = RefundWorkTimeWeekDay + item.RefundTimeWeekDay + ",";
}
//休息日
for (int i = ; i < ; i++)
{
WorkTimeWeekDay = WorkTimeWeekDay + item.WorkTimePlayDay + ",";
RefundWorkTimeWeekDay = RefundWorkTimeWeekDay + item.RefundTimePlayDay + ",";
} info.Append("<Item Id='" + item.ID + "' State='" + item.Status + "' IsChangePnr='" + strChangePnr + "' ");
info.Append("ProviderWorkTime='" + WorkTimeWeekDay.Trim(',') + "' PorviderVWorkTime='" + RefundWorkTimeWeekDay.Trim(',') + "'>");
info.Append(item.DepartCode.Replace("/", ",") + "|" + item.ArriveCode.Replace("/", ",") + "|" + item.AirCompany + "|" + item.TripType + "|");
info.Append(ZtFlightNumber + "|" + ZtNoFlightNumber + "|" + strWeekNum + "|" + item.TicketType + "|");
info.Append(strCabin + "|" + c.ToString("0.000") + "|" + item.SaleBeginTime.ToString("yyyy-MM-dd") + ",");
info.Append(item.SaleEndTime.ToString("yyyy-MM-dd") + "|" + item.TravelBeginTime.ToString("yyyy-MM-dd") + ",");
info.Append(item.TravelEndTime.ToString("yyyy-MM-dd") + "|" + item.OfficeID + "|");
info.Append(item.AddTime.ToString("yyyy-MM-ddTHH:mm:ss.fff") + "|" + item.ModifyTime.ToString("yyyy-MM-ddTHH:mm:ss.fff") + "|");
info.Append(item.SupplierCode + "|" + item.Remark + "|" + d);
info.Append("</Item>"); //同时remove item
zrate.RateList.RemoveAt(index);
//每10000条调用一次gc,回收前一百条被remove的数据
if (0 == index % 10000)
{
GC.Collect();
}
}
}
else
{
log.Error("zrate里的RateList为空!");
}
}
else
{
info.Append("<OTA_Alter_AirFareRS>");
info.Append("<Error Code=\"" + zrate.Msg + "\" ShortText=\"" + zrate.Msg + "\" />");
}
info.Append("</OTA_Alter_AirFareRS>"); //WriteLog.WriteErrorLogDataInteraction(info.ToString(), "变动政策返回信息");
log.Debug("返回xml成功");
return info.ToString();