修改可用内存策略

This commit is contained in:
2248356998 qq.com
2025-08-01 12:43:39 +08:00
parent 10391f869b
commit 3cc9d31f28
3 changed files with 33 additions and 10 deletions

View File

@@ -683,13 +683,36 @@ public class MachineInfo : IExtend
if (dic.TryGetValue("MemTotal", out var str) && !str.IsNullOrEmpty())
Memory = (UInt64)str.TrimEnd(" kB").ToLong();
if (dic.TryGetValue("MemAvailable", out str) && !str.IsNullOrEmpty())
AvailableMemory = (UInt64)str.TrimEnd(" kB").ToLong();
else if (dic.TryGetValue("MemFree", out str) && !str.IsNullOrEmpty())
AvailableMemory =
(UInt64)(str.TrimEnd(" kB").ToLong() +
dic["Buffers"]?.TrimEnd(" kB").ToLong() ?? 0 +
dic["Cached"]?.TrimEnd(" kB").ToLong() ?? 0);
/*
指标 含义 是否可回收
MemTotal 系统总物理内存 ❌ 固定值
MemFree 完全未使用的内存(不含缓存/缓冲区) ✅ 100% 可用
MemAvailable 内核估计的可用内存(含可回收缓存和缓冲区) ✅ 最权威的保守估计
Cached 文件系统缓存Page Cache可完全回收 ✅ 100% 可回收
SReclaimable Slab 缓存中可回收的部分(如 dentry/inode ✅ 大部分可回收80%~90%
Buffers 磁盘块缓存(现代内核中值较小,可回收) ✅ 可回收
Slab 内核对象缓存总大小(含可回收和不可回收部分) ⚠️ 需区分 SReclaimable
SwapCached 被缓存到 Swap 的内存(可回收,但性能较差) ✅ 可回收但不建议依赖
*/
var ma = (UInt64)(dic["MemAvailable"]?.TrimEnd(" kB").ToLong() ?? 0);
var mf = (UInt64)(dic["MemFree"]?.TrimEnd(" kB").ToLong() ?? 0);
var mc = (UInt64)(dic["Cached"]?.TrimEnd(" kB").ToLong() ?? 0);
if (dic.TryGetValue("SReclaimable", out str) && !str.IsNullOrEmpty())
{
var sr = (UInt64)(str?.TrimEnd(" kB").ToLong() ?? 0);
mf += (ulong)(sr * 0.9);
}
if (dic.TryGetValue("Buffers", out str) && !str.IsNullOrEmpty())
{
var bf = (UInt64)(str?.TrimEnd(" kB").ToLong() ?? 0);
mf += bf;
}
mf += mc;
AvailableMemory = ma > mf ? ma : mf;
}
// A2/A4温度获取BuildrootCPU温度和主板温度

View File

@@ -39,8 +39,7 @@ public abstract class DriverBase : DisposableObject, IDriver
/// <summary>
/// 当前设备
/// </summary>
public DeviceRuntime? CurrentDevice => WeakReferenceCurrentDevice?.TryGetTarget(out var target) == true ? target : null;
private WeakReference<DeviceRuntime> WeakReferenceCurrentDevice { get; set; }
public DeviceRuntime? CurrentDevice { get; private set; }
/// <summary>
/// 当前设备Id
/// </summary>
@@ -208,7 +207,7 @@ public abstract class DriverBase : DisposableObject, IDriver
/// </summary>
internal void InitDevice(DeviceRuntime device)
{
WeakReferenceCurrentDevice = new WeakReference<DeviceRuntime>(device);
CurrentDevice = device;
_logger = App.RootServices.GetService<Microsoft.Extensions.Logging.ILoggerFactory>().CreateLogger($"Driver[{CurrentDevice.Name}]");

View File

@@ -505,6 +505,7 @@ public partial class MqttClient : BusinessBaseWithCacheIntervalScriptAll
private async ValueTask<OperResult> TryMqttClientAsync(CancellationToken cancellationToken)
{
System.Console.WriteLine($"{this.DeviceName} TryMqttClientAsync {DateTime.Now:HH:mm:ss.fff}");
if (_mqttClient?.IsConnected == true)
return OperResult.Success;
return await Client().ConfigureAwait(false);