Fixed memory counter not refreshing over time

This commit is contained in:
Kaoticz
2021-10-17 22:56:10 +00:00
committed by Kwoth
parent 24a4745193
commit 92365fd22d
3 changed files with 19 additions and 9 deletions

View File

@@ -253,7 +253,7 @@ namespace NadekoBot.Modules.Utility
.AddField(GetText(strs.commands_ran), _stats.CommandsRan.ToString(), true)
.AddField(GetText(strs.messages), $"{_stats.MessageCounter} ({_stats.MessagesPerSecond:F2}/sec)",
true)
.AddField(GetText(strs.memory), FormattableString.Invariant($"{_stats.PrivateMemory:F2} MB"), true)
.AddField(GetText(strs.memory), FormattableString.Invariant($"{_stats.GetPrivateMemory():F2} MB"), true)
.AddField(GetText(strs.owner_ids), ownerIds, true)
.AddField(GetText(strs.uptime), _stats.GetUptimeString("\n"), true)
.AddField(GetText(strs.presence),

View File

@@ -14,11 +14,6 @@ namespace NadekoBot.Services
/// </summary>
long CommandsRan { get; }
/// <summary>
/// The total amount of private memory used by the bot, in Megabytes.
/// </summary>
double PrivateMemory { get; }
/// <summary>
/// The Discord framework used by the bot.
/// </summary>
@@ -54,5 +49,10 @@ namespace NadekoBot.Services
/// </summary>
/// <param name="separator">The formatting separator.</param>
string GetUptimeString(string separator = ", ");
/// <summary>
/// Gets total amount of private memory currently in use by the bot, in Megabytes.
/// </summary>
double GetPrivateMemory();
}
}

View File

@@ -13,7 +13,7 @@ using System.Threading.Tasks;
namespace NadekoBot.Services
{
public class StatsService : IStatsService, IReadyExecutor, INService
public class StatsService : IStatsService, IReadyExecutor, INService, IDisposable
{
private readonly Process _currentProcess = Process.GetCurrentProcess();
private readonly DiscordSocketClient _client;
@@ -23,8 +23,6 @@ namespace NadekoBot.Services
public const string BotVersion = "3.0.7";
public string Author => "Kwoth#2452";
public string Library => "Discord.Net";
public double PrivateMemory => _currentProcess.PrivateMemorySize64 / (double)1.MiB();
public double MessagesPerSecond => MessageCounter / GetUptime().TotalSeconds;
private long _textChannels;
@@ -173,5 +171,17 @@ namespace NadekoBot.Services
_voiceChannels = guilds.Sum(g => g.Channels.Count(cx => cx is IVoiceChannel));
return Task.CompletedTask;
}
public double GetPrivateMemory()
{
_currentProcess.Refresh();
return _currentProcess.PrivateMemorySize64 / (double)1.MiB();
}
public void Dispose()
{
_currentProcess.Dispose();
GC.SuppressFinalize(this);
}
}
}