C# Aysnc任务——我的应用程序&x27;s的内存消耗越来越慢,为什么?
我使用“Task.Factory.ContinueWhenAll()”来提高应用程序的性能,但我遇到了一个问题,内存正在慢慢变高 然后我使用GC.collect()和GC.GC.WaitForPendingFinalizers()得到了相同的情况C# Aysnc任务——我的应用程序&x27;s的内存消耗越来越慢,为什么?,c#,C#,我使用“Task.Factory.ContinueWhenAll()”来提高应用程序的性能,但我遇到了一个问题,内存正在慢慢变高 然后我使用GC.collect()和GC.GC.WaitForPendingFinalizers()得到了相同的情况 int taskCount = Settings.Default.TaskCount; int monitorInterval = Settings.Default.MonitorInterval; whil
int taskCount = Settings.Default.TaskCount;
int monitorInterval = Settings.Default.MonitorInterval;
while (true)
{
using (TaskHandleSvr taskItem = new TaskHandleSvr())
{
List<Task> taskList = new List<Task>();
for (int i = 0; i < taskCount; i++)
taskList.Add(taskItem.RegisterRun());
await Task.Factory.ContinueWhenAll(taskList.ToArray(), wordCountTasks =>
{
int count = taskItem.ProcessMisseHandledRedisMsg();
string sLogTime = $"{DateTime.UtcNow.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss.ffff")}";
string sLogName = "-";
string sLogMessage = "Service End!";
NLogHelper.Info($"{sLogName}*|*{sLogTime}*|*Loop{count}Msg");
NLogHelper.Info($"{sLogName}*|*{sLogTime}*|*{sLogMessage}");
});
taskItem.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Console.ReadKey();
await Task.Delay(TimeSpan.FromSeconds(5));
}
int taskCount=Settings.Default.taskCount;
int monitorienterval=Settings.Default.monitorienterval;
while(true)
{
使用(TaskHandleSvr taskItem=new TaskHandleSvr())
{
List taskList=新列表();
对于(int i=0;i
{
int count=taskItem.ProcessMisseHandledRedisMsg();
字符串sLogTime=$“{DateTime.UtcNow.AddHours(8).ToString(“yyyy-MM-dd-HH:MM:ss.ffff”)}”;
字符串sLogName=“-”;
字符串sLogMessage=“服务结束!”;
NLogHelper.Info($“{sLogName}*.*{sLogTime}*.*Loop{count}Msg”);
NLogHelper.Info($“{sLogName}*{sLogTime}*{sLogMessage}”);
});
taskItem.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Console.ReadKey();
等待任务延迟(时间跨度从秒(5));
}
//下面是TaskHandleSvr我在这里!最后,我把我的错误修好了。哇~
protected override async Task RunAsync(CancellationToken cancellationToken)
{
int taskCount= Settings.Default.TaskCount;
int monitorIntervalSeconds = Settings.Default.MonitorInterval;
List<Task> taskList = new List<Task>();
while (true)
{
for (int i = 0; i < taskCount; i++)
taskList.Add(Task.Factory.StartNew(fn => { TaskHandleSvr.RegisterRun().Wait(); }, null));
await Task.Factory.ContinueWhenAll(taskList.ToArray(), wordCountTasks =>
{
int count = TaskHandleSvr.ProcessMisseHandledRedisMsg().Result;
string sLogTime = $"{DateTime.UtcNow.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss.ffff")}";
string sLogName = "--myTaskGroup--";
string sLogMessage = "Task End";
NLogHelper.Info($"{sLogName}*|*{sLogTime}*|*RollBack {count} Message");
NLogHelper.Info($"{sLogName}*|*{sLogTime}*|*{sLogMessage}");
});
//Release Resource
foreach (var item in taskList)
item.Dispose();
taskList.RemoveRange(0, taskList.Count);
await Task.Delay(TimeSpan.FromSeconds(monitorIntervalSeconds), cancellationToken);
}
}
受保护的覆盖异步任务RunAsync(CancellationToken CancellationToken)
{
int taskCount=Settings.Default.taskCount;
int monitorientervalseconds=Settings.Default.monitorienterval;
List taskList=新列表();
while(true)
{
对于(int i=0;i{TaskHandleSvr.RegisterRun().Wait();},null));
wait Task.Factory.continuewhell(taskList.ToArray(),wordCountTasks=>
{
int count=TaskHandleSvr.ProcessMisseHandledRedisMsg().Result;
字符串sLogTime=$“{DateTime.UtcNow.AddHours(8).ToString(“yyyy-MM-dd-HH:MM:ss.ffff”)}”;
字符串sLogName=“--myTaskGroup--”;
字符串sLogMessage=“任务结束”;
NLogHelper.Info($“{sLogName}*.*{sLogTime}*.*回滚{count}消息”);
NLogHelper.Info($“{sLogName}*{sLogTime}*{sLogMessage}”);
});
//释放资源
foreach(任务列表中的变量项)
item.Dispose();
taskList.RemoveRange(0,taskList.Count);
等待任务延迟(TimeSpan.FromSeconds(monitorIntervalSeconds),cancellationToken);
}
}
然后我使用GC.collect()和GC.GC.WaitForPendingFinalizers(),我得到了相同的情况
这到底意味着什么,如果您认为您有内存泄漏,还可以使用内存探查器平均任务计数
可以得到多大?您确定要在任务项.dispose()中处理所有内容吗代码>?taskitem似乎正在增长且未被分派任务管理器不是合适的内存分析器,它使用了错误的统计数据。五秒钟太长了,根本看不到GC内存使用的典型锯齿模式。删除Task.Delay()、删除GC.Collect()、使用Perfmon.exe查看.NET内存计数器,让它在周末运行。然后使用内存分析器。