- 项目类型:Windows Service
- .net Version: .net Framework 4.8
- 开发语言: C#
- influxDB API: InfluxDB.Client v4.15.0 InfluxDB.Client.Core v4.15.0
重现代码:
创建一个Timer周期性地调用写入函数:
_timerWriteDB = new Timer((o) => WriteData(), null, timeMinute * 1000, 60 * 1000); // per 1 minutes
WriteData的实现:
private async void WriteData() { try { lock (syncTable) { string now = DateTime.Now.ToString("HH:mm:ss"); foreach (DataRow r in deviceWorkHour.Rows) { if (string.Compare(now, r["startTime"].ToString()) >= 0 && string.Compare(now, r["endtime"].ToString()) < 0) { var point = PointData.Measurement(Measurement) .Tag("plantcode", r["plantcode"].ToString()) .Tag("workshopCode", r["workshopCode"].ToString()) .Tag("linecode", r["linecode"].ToString()) .Tag("groupcode", r["groupcode"].ToString()) .Tag("WorkTimeType", r["WorkTimeType"].ToString()) .Field("schdTime", 60); //每分钟写一次,即每次60s var writeApi = _influxDBClient.GetWriteApi(); writeApi.WritePoint(point, bucket, org); _logger.Info($"log schdTime for {r["groupcode"]}"); } } } } catch (Exception ex) { _logger.Error(ex.ToString()); } }
修改办法,将异步方法 GetWriteApi 修改为同步方法 GetWriteApiAsync
private void WriteData() { try { lock (syncTable) { string now = DateTime.Now.ToString("HH:mm:ss"); List<PointData> points = new List<PointData>(); foreach (DataRow r in deviceWorkHour.Rows) { if (string.Compare(now, r["startTime"].ToString()) >= 0 && string.Compare(now, r["endtime"].ToString()) < 0) { var point = PointData.Measurement(Measurement) .Tag("plantcode", r["plantcode"].ToString()) .Tag("workshopCode", r["workshopCode"].ToString()) .Tag("linecode", r["linecode"].ToString()) .Tag("groupcode", r["groupcode"].ToString()) .Tag("WorkTimeType", r["WorkTimeType"].ToString()) .Field("schdTime", 60); //每分钟写一次,即每次60s points.Add(point); } } var writeApi = _influxDBClient.GetWriteApiAsync(); writeApi.WritePointsAsync(points, bucket, org); } } catch (Exception ex) { _logger.Error(ex.ToString()); } }
总结,这应该是一个bug。
标签:泄漏,workshopCode,string,Influxdb,Tag,ToString,内存,groupcode,now From: https://www.cnblogs.com/crazyghostvon/p/18230816