Merge branch 'memfix' into 'v3'

Fixed memory counter not refreshing over time

See merge request Kwoth/nadekobot!177
This commit is contained in:
Kwoth
2021-10-17 22:56:10 +00:00
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.commands_ran), _stats.CommandsRan.ToString(), true)
.AddField(GetText(strs.messages), $"{_stats.MessageCounter} ({_stats.MessagesPerSecond:F2}/sec)", .AddField(GetText(strs.messages), $"{_stats.MessageCounter} ({_stats.MessagesPerSecond:F2}/sec)",
true) 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.owner_ids), ownerIds, true)
.AddField(GetText(strs.uptime), _stats.GetUptimeString("\n"), true) .AddField(GetText(strs.uptime), _stats.GetUptimeString("\n"), true)
.AddField(GetText(strs.presence), .AddField(GetText(strs.presence),

View File

@@ -14,11 +14,6 @@ namespace NadekoBot.Services
/// </summary> /// </summary>
long CommandsRan { get; } long CommandsRan { get; }
/// <summary>
/// The total amount of private memory used by the bot, in Megabytes.
/// </summary>
double PrivateMemory { get; }
/// <summary> /// <summary>
/// The Discord framework used by the bot. /// The Discord framework used by the bot.
/// </summary> /// </summary>
@@ -54,5 +49,10 @@ namespace NadekoBot.Services
/// </summary> /// </summary>
/// <param name="separator">The formatting separator.</param> /// <param name="separator">The formatting separator.</param>
string GetUptimeString(string separator = ", "); 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 namespace NadekoBot.Services
{ {
public class StatsService : IStatsService, IReadyExecutor, INService public class StatsService : IStatsService, IReadyExecutor, INService, IDisposable
{ {
private readonly Process _currentProcess = Process.GetCurrentProcess(); private readonly Process _currentProcess = Process.GetCurrentProcess();
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
@@ -23,8 +23,6 @@ namespace NadekoBot.Services
public const string BotVersion = "3.0.7"; public const string BotVersion = "3.0.7";
public string Author => "Kwoth#2452"; public string Author => "Kwoth#2452";
public string Library => "Discord.Net"; public string Library => "Discord.Net";
public double PrivateMemory => _currentProcess.PrivateMemorySize64 / (double)1.MiB();
public double MessagesPerSecond => MessageCounter / GetUptime().TotalSeconds; public double MessagesPerSecond => MessageCounter / GetUptime().TotalSeconds;
private long _textChannels; private long _textChannels;
@@ -173,5 +171,17 @@ namespace NadekoBot.Services
_voiceChannels = guilds.Sum(g => g.Channels.Count(cx => cx is IVoiceChannel)); _voiceChannels = guilds.Sum(g => g.Channels.Count(cx => cx is IVoiceChannel));
return Task.CompletedTask; return Task.CompletedTask;
} }
public double GetPrivateMemory()
{
_currentProcess.Refresh();
return _currentProcess.PrivateMemorySize64 / (double)1.MiB();
}
public void Dispose()
{
_currentProcess.Dispose();
GC.SuppressFinalize(this);
}
} }
} }