forked from ddrilling/AsbCloudServer
Add memory monitor
This commit is contained in:
parent
8e9baf22d8
commit
b05aadccb4
@ -9,6 +9,7 @@ using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using AsbCloudInfrastructure.Background;
|
||||
|
||||
namespace AsbCloudInfrastructure
|
||||
{
|
||||
@ -30,9 +31,42 @@ namespace AsbCloudInfrastructure
|
||||
backgroundWorker.Push(OperationDetectionWorkFactory.MakeWork());
|
||||
backgroundWorker.Push(SubsystemOperationTimeCalcWorkFactory.MakeWork());
|
||||
backgroundWorker.Push(LimitingParameterCalcWorkFactory.MakeWork());
|
||||
backgroundWorker.Push(MakeMemoryMonitoringWork());
|
||||
|
||||
Task.Delay(1_000)
|
||||
.ContinueWith(async (_) => await backgroundWorker.StartAsync(CancellationToken.None));
|
||||
}
|
||||
|
||||
static WorkPeriodic MakeMemoryMonitoringWork()
|
||||
{
|
||||
var workId = "Memory monitoring";
|
||||
var workAction = (string _, IServiceProvider _, CancellationToken _) => {
|
||||
var bytes = GC.GetTotalMemory(false);
|
||||
var bytesString = FromatBytes(bytes);
|
||||
System.Diagnostics.Trace.TraceInformation($"Total memory allocated is {bytesString} bytes");
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
var workPeriod = TimeSpan.FromMinutes(1);
|
||||
var work = new WorkPeriodic(workId, workAction, workPeriod);
|
||||
return work;
|
||||
}
|
||||
|
||||
static string FromatBytes(long bytes)
|
||||
{
|
||||
const double gigaByte = 1024 * 1024 * 1024;
|
||||
const double megaByte = 1024 * 1024;
|
||||
const double kiloByte = 1024;
|
||||
|
||||
if (bytes > 10 * gigaByte)
|
||||
return (bytes / gigaByte).ToString("### ### ###.## Gb");
|
||||
|
||||
if (bytes > 10 * megaByte)
|
||||
return (bytes / megaByte).ToString("### ### ###.## Mb");
|
||||
|
||||
if (bytes > 10 * kiloByte)
|
||||
return (bytes / megaByte).ToString("### ### ###.## Kb");
|
||||
|
||||
return bytes.ToString("### ### ###");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user