ASP.NET Azure WebApp TimerQueueTimer内存/ CPU使用率问题

时间:2018-10-20 10:43:53

标签: c# asp.net memory-leaks timer cpu-usage

我正在基于经典ASP.NET(4.6.1)的azure构建上托管一个Web应用程序。 我的问题是CPU和内存使用率增加。

应用程序是由应用程序使用的API。在推送过程中,我们至少在2-5秒钟内命中了每秒100-3000个用户。

获得完整的转储和dotMemory试用版后,我看到以下图片。 enter image description here

我有两个使用计时器机制写数据的类。

统计数据

public sealed class Statistics : IStatistics, IDisposable
{
    private static Lazy<Statistics> _lazy => new Lazy<Statistics>(() => new Statistics());
    public static Statistics Instance => Statistics._lazy.Value;

    private readonly object _mutex = new object();
    private readonly IDictionary<string, LogObject> _cache;

    private readonly TimeSpan _interval;
    private readonly Task _outputTask;
    private readonly CancellationTokenSource _cancellationTokenSource;

    private Statistics()
    {
        this._interval = TimeSpan.FromSeconds(15);
        this._cache = new Dictionary<string, LogObject>();
        this._cancellationTokenSource = new CancellationTokenSource();
        this._outputTask = Task.Factory.StartNew(
            this.WriteToRedis,
            null,
            TaskCreationOptions.LongRunning
        );
    }

    private void Stop()
    {
        this._cancellationTokenSource.Cancel();

        try
        {
            this._outputTask.Wait(this._interval);
        }
        catch (TaskCanceledException)
        {
        }
        catch (AggregateException ex) when (ex.InnerExceptions.Count == 1 && ex.InnerExceptions[0] is TaskCanceledException)
        {
        }
        this._cancellationTokenSource?.Dispose();
    }

    private static Task IntervalAsync(TimeSpan interval, CancellationToken cancellationToken)
        => Task.Delay(interval, cancellationToken);

    public async Task WriteToRedis(object state)
    {
        while (!this._cancellationTokenSource.IsCancellationRequested)
        {
            // write stuff to redis in azure

            await IntervalAsync(this._interval, this._cancellationTokenSource.Token);
        }
    }

    public Task ReportQuery(string query, string data)
    {
        // save data to local cache
    }

    public void Dispose()
        => this.Stop();
}

我第二次使用相同的机制是用于保存字典的本地缓存类。计时器会清除字典键以确保设置的有效期。

有人知道为什么TimerQueueTimer对象增加该数量吗?我想念一切吗?

预先感谢

0 个答案:

没有答案