none
内存飙升 RRS feed

  • 问题

  • /// <summary>
            /// 计算天数据
            /// </summary>
            private static void ComputeDayData(DateTime sTime, DateTime eTime)
            {
                var stTime = Convert.ToDateTime(sTime.ToString("yyyy-MM-dd"));
                var edTime = Convert.ToDateTime(eTime.AddDays(1).ToString("yyyy-MM-dd"));
    
                var listData = new List<DayRecord>();
    
                while (stTime < edTime)
                {
                    var data = DatenauswertungDataService.GetAutoDataService().GetAutoDatas(stTime, stTime.AddDays(1));
                    var datas = GetAddDatas(stTime, stTime.AddDays(1), data);
    
                    //获得当天所有仪表的环比数据
                    var sDatas = DatenauswertungDataService.GetDayRecordService().GetDayRecordsByTime(stTime.AddMonths(-1));
    
                    //获得当前所有仪表的同比数据
                    var lDatas = DatenauswertungDataService.GetDayRecordService().GetDayRecordsByTime(stTime.AddYears(-1));
    
                    foreach (var objectse in datas)
                    {
                        var dr = new DayRecord() { TotalData = Convert.ToDouble(objectse[0]), TotalMoney = Convert.ToDouble(objectse[1]), HTime = stTime, BaseMeterFieldType = new MeterFieldType() { Id = Convert.ToInt32(objectse[4]) } };
    
                        var sData = sDatas.FirstOrDefault(a => a.BaseMeterFieldType.Id == dr.BaseMeterFieldType.Id);
                        var lData = lDatas.FirstOrDefault(a => a.BaseMeterFieldType.Id == dr.BaseMeterFieldType.Id);
    
                        if (sData != null)
                        {
                            dr.SameCompareTotalData = ComputeDataComparison(dr.TotalData, sData.TotalData);
                            dr.SameCompareTotalMoney = ComputeDataComparison(dr.TotalMoney, sData.TotalMoney);
                        }
                        else
                        {
                            dr.SameCompareTotalMoney = 0;
                            dr.SameCompareTotalData = 0;
                        }
    
                        if (lData != null)
                        {
                            dr.LinkCompareTotalData = ComputeDataComparison(dr.TotalData, lData.TotalData);
                            dr.LinkCompareTotalMoney = ComputeDataComparison(dr.TotalMoney, lData.TotalMoney);
                        }
                        else
                        {
                            dr.LinkCompareTotalData = 0;
                            dr.LinkCompareTotalMoney = 0;
                        }
                        listData.Add(dr);
                    }
    
                    stTime = stTime.AddDays(1);
                    Console.WriteLine(string.Format("Day=>{0}", stTime.ToString("yyyy-MM-dd")));
    
                    DatenauswertungDataService.GetDayRecordService().CreateDayRecords(listData);
                    listData.Clear();
                }
                //计算月和年数据
                //new Task(() => ComputeMonthData(sTime, eTime)).Start();
                //new Task(() => ComputeYearData(sTime, eTime)).Start();
            }

    /// <summary>
            /// 返回时间点对应的仪表用量和费用
            /// </summary>
            /// <param name="sTime"></param>
            /// <param name="eTime"></param>
            /// <param name="autoDatas"></param>
            /// <returns></returns>
            private static IEnumerable<object[]> GetAddDatas(DateTime sTime, DateTime eTime, ICollection<AutoData> autoDatas)
            {
                //返回结果
                var result = new List<object[]>();
    
                //遍历所有的仪表的价格
                foreach (var meter in _meters)
                {
                    var fieldTypes = _meterFieldTypes.Where(a => a.Meter.Id == meter.Id);
    
                    //获得仪表对应的价格的信息
                    var ptcs = _mPriceTypeConfigs.Where(a => a.PriceType.Id == meter.PriceType.Id);
    
                    foreach (var bitem in fieldTypes)
                    {
                        //费用
                        double cost = 0;
    
                        //得到离开始时间最近的一条数据,如果没有数据,系统自动判断为开始采集,读取表的起始值
                        AutoData at = DatenauswertungDataService.GetAutoDataService().GetAutoDataByTime(sTime, bitem.Id);
                        double startMeterValue = at != null ? at.MeterValue : meter.InitialValue;
    
                        //获得开始用量
                        //获得时间段内的数据  //现在根据累计值才进行处理
                        //var aDatas = DatenauswertungDataService.GetAutoDataService().GetAutoDatasByMeterIdAndCumulativeValue(sTime, eTime, bitem.Id);
                        var aDatas = autoDatas.Where(a => a.RecpodTime >= sTime && a.RecpodTime < eTime && a.MeterFieldType.Id == bitem.Id);
    
                        //分组统计
                        foreach (AutoData aditem in aDatas)
                        {
                            //获得时间所在价格类型信息
                            var v = ptcs.Where(p => p.StartDateTime <= aditem.RecpodTime && p.EndDateTime >= aditem.RecpodTime).ToList();
                            //价格
                            double price = 0;
                            //如果没有设置这段时间内的价格,则不计算数据
                            if (v.Any())
                            {
                                var ptc = v.FirstOrDefault();
    
                                //获取价格,是否是统一价格还是分段价格
                                if (ptc.IsOnly)
                                {
                                    price = ptc.OnlyPrice;
                                }
                                else
                                {
                                    var pric = ptc.PriceTypeConfigTypeOnlies.FirstOrDefault(p =>
                                        Convert.ToDateTime(p.StartDateTime.ToString("HH:mm:ss")) <=
                                        Convert.ToDateTime(aditem.RecpodTime.ToString("HH:mm:ss")) &&
                                        Convert.ToDateTime(p.EndDateTime.ToString("HH:mm:ss")) >=
                                        Convert.ToDateTime(aditem.RecpodTime.ToString("HH:mm:ss")));
                                    if (pric != null)
                                    {
                                        price = pric.Price;
                                    }
                                }
                            }
    
                            //起始值大于后面的值,说明是错误数据,不进行数据计算
                            if (aditem.MeterValue > startMeterValue)
                            {
                                cost += (aditem.MeterValue - startMeterValue) * price;
                                //dosage = (item.MeterValue - startMeterValue);
                            }
    
                            startMeterValue = aditem.MeterValue;
                        }
    
                        var orderData = aDatas.OrderBy(a => a.RecpodTime);
                        //用量
                        double dosage = 0;
                        if (orderData.Any())
                        {
                            dosage = orderData.LastOrDefault().MeterValue - orderData.FirstOrDefault().MeterValue;
                        }
                        result.Add(new object[] { dosage, cost, sTime, eTime, bitem.Id });
                    }
                }
                return result;
            }


    2013年12月24日 8:51

答案

  • 你好,谢谢你的回复

    我测试了下,内存猛增是因为下面一段代码造成的

    var data = DatenauswertungDataService.GetAutoDataService().GetAutoDatas(stTime, stTime.AddDays(1));

    看了占的内存,是因为访问WCF这个方法,造成了wcf那个服务占内存很大

    2013年12月25日 2:40

全部回复

  • 你好,

    楼主试下把

    private static IEnumerable<object[]> GetAddDatas(DateTime sTime, DateTime eTime, ICollection<AutoData> autoDatas)

    方法换成IQueryable类型, 返回结果也试着用var result = new IQueryable<object[]>()这个类型.

    IEnumerable会把数据都load到内存,在内存中进行筛选, tolist就直接load数据到本地了.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年12月25日 1:35
    版主
  • 你好,谢谢你的回复

    我测试了下,内存猛增是因为下面一段代码造成的

    var data = DatenauswertungDataService.GetAutoDataService().GetAutoDatas(stTime, stTime.AddDays(1));

    看了占的内存,是因为访问WCF这个方法,造成了wcf那个服务占内存很大

    2013年12月25日 2:40