#region S# License
/******************************************************************************************
NOTICE!!! This program and source code is owned and licensed by
StockSharp, LLC, www.stocksharp.com
Viewing or use of this code requires your acceptance of the license
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
Removal of this comment is a violation of the license agreement.
Project: StockSharp.Logging.Logging
File: MemoryStatistics.cs
Created: 2015, 11, 11, 2:32 PM
Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License
namespace StockSharp.Logging
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Ecng.Collections;
using Ecng.Common;
using Ecng.Configuration;
using Ecng.Serialization;
using MoreLinq;
///
/// The class for tracking objects taking in memory.
///
public sealed class MemoryStatistics : BaseLogReceiver
{
static MemoryStatistics()
{
Instance = new MemoryStatistics();
}
///
/// An object of class .
///
public static MemoryStatistics Instance { get; }
private readonly Timer _timer;
private MemoryStatistics()
{
var lastTime = DateTime.Now;
_timer = ThreadingHelper.Timer(() =>
{
if (IsDisposed)
return;
if (DateTime.Now - lastTime < Interval)
return;
lastTime = DateTime.Now;
this.AddInfoLog(ToString());
}).Interval(Interval);
}
///
/// Release resources.
///
protected override void DisposeManaged()
{
if (IsDisposed)
return;
_timer.Dispose();
base.DisposeManaged();
}
private TimeSpan _interval = TimeSpan.FromSeconds(60);
///
/// Statistics logging interval. The default is 1 minute.
///
public TimeSpan Interval
{
get => _interval;
set
{
if (_interval == value)
return;
_interval = value;
_timer.Change(value, value);
}
}
private readonly CachedSynchronizedSet _values = new CachedSynchronizedSet();
///
/// Monitored objects.
///
public IList Values => _values;
///
/// Save settings.
///
/// Settings storage.
public override void Save(SettingsStorage storage)
{
base.Save(storage);
storage.SetValue(nameof(Interval), Interval);
}
///
/// Load settings.
///
/// Settings storage.
public override void Load(SettingsStorage storage)
{
base.Load(storage);
Interval = storage.GetValue(nameof(Interval));
}
///
/// To clear memory statistics.
///
/// Whether to clear the objects counter.
public void Clear(bool resetCounter)
{
_values.Cache.ForEach(v => v.Clear(resetCounter));
}
///
/// Returns a string that represents the current object.
///
/// A string that represents the current object.
public override string ToString()
{
return _values.Select(v => "{0} = {1}".Put(v.Name, v.ObjectCount)).Join(", ");
}
///
/// Is the source on.
///
public static bool IsEnabled => ConfigManager.GetService().Sources.OfType().Any();
///
/// To add or to remove the source from the registered .
///
public static void AddOrRemove()
{
var sources = ConfigManager.GetService().Sources;
var stat = sources.OfType().ToArray();
if (stat.Any())
sources.RemoveRange(stat);
else
sources.Add(Instance);
}
}
}