Skip to content

Commit

Permalink
Windowsでのメモリサイズの取得にSystem.Managementを使うようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Feb 4, 2024
1 parent 0bb3dcb commit 6838cb9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="System.Interactive" Version="6.0.1" />
<PackageVersion Include="System.Interactive.Async" Version="6.0.1" />
<PackageVersion Include="System.Management" Version="8.0.0" />
<PackageVersion Include="System.Reactive" Version="6.0.0" />
<PackageVersion Include="Vortice.XAudio2" Version="3.3.4" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/Beutl.Configuration/Beutl.Configuration.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="System.Management" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Beutl.Core\Beutl.Core.csproj" />
</ItemGroup>
Expand Down
37 changes: 18 additions & 19 deletions src/Beutl.Configuration/EditorConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Management;
using System.Reflection;
using System.Runtime.Loader;
using System.Runtime.Versioning;

using Beutl.Collections;
using Beutl.Serialization;
Expand Down Expand Up @@ -99,7 +103,7 @@ public bool IsAutoSaveEnabled
get => GetValue(IsAutoSaveEnabledProperty);
set => SetValue(IsAutoSaveEnabledProperty, value);
}

public bool IsFrameCacheEnabled
{
get => GetValue(IsFrameCacheEnabledProperty);
Expand All @@ -117,7 +121,7 @@ public FrameCacheConfigScale FrameCacheScale
get => GetValue(FrameCacheScaleProperty);
set => SetValue(FrameCacheScaleProperty, value);
}

public FrameCacheConfigColorType FrameCacheColorType
{
get => GetValue(FrameCacheColorTypeProperty);
Expand Down Expand Up @@ -164,31 +168,26 @@ public override void Deserialize(ICoreSerializationContext context)
}
}

[SupportedOSPlatform("windows")]
private static ulong GetWindowsMemoryCapacity()
{
// wmic memorychip get capacity
using var process = Process.Start(new ProcessStartInfo("wmic", "memorychip get capacity")
try
{
CreateNoWindow = true,
RedirectStandardOutput = true
});
using var mc = new ManagementClass("Win32_OperatingSystem");
using ManagementObjectCollection moc = mc.GetInstances();

if (process != null && process.WaitForExit(500))
{
ulong value = 0;
while (process.StandardOutput.ReadLine() is string line)
ulong total = 0;
foreach (ManagementBaseObject? mo in moc)
{
if (ulong.TryParse(line, out ulong v))
{
value += v;
}
total += Convert.ToUInt64(mo["TotalVisibleMemorySize"]);
}

return value;
return total * 1024;
}
catch
{
return 4L * 1024 * 1024 * 1024;
}

// https://www.microsoft.com/ja-jp/windows/windows-11-specifications
return 4L * 1024 * 1024 * 1024;
}

private static ulong GetLinuxMemoryCapacity()
Expand Down

0 comments on commit 6838cb9

Please sign in to comment.