Skip to content

Commit

Permalink
get TotalVisibleMemorySize
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn committed Nov 4, 2023
1 parent f35ccb4 commit 366cd9c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
39 changes: 27 additions & 12 deletions OneMore/AddIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ public AddIn()
thread.CurrentUICulture = Culture;

var (cpu, ram) = GetMachineProps();
var uram = ram > ulong.MaxValue ? ">GB" : ((ulong)ram).ToBytes();

logger.WriteLine();
logger.Start(
$"Starting {process.ProcessName} {process.Id}, {cpu} Mhz, {ram.ToBytes()}, " +
$"Starting {process.ProcessName} {process.Id}, {cpu} Mhz, {uram}, " +
$"{thread.CurrentCulture.Name}/{thread.CurrentUICulture.Name}, " +
$"v{AssemblyInfo.Version}, OneNote {Office.GetOneNoteVersion()}, " +
$"Office {Office.GetOfficeVersion()}, " +
Expand Down Expand Up @@ -145,28 +146,42 @@ private void CatchUnhandledException(object sender, UnhandledExceptionEventArgs



private (uint, ulong) GetMachineProps()
private (uint, double) GetMachineProps()
{
static T Query<T>(string field, string table)
// using this as a means of short-circuiting the Ensure methods for slower machines
// to speed up the display of the menus. CurrentClockSpeed will vary depending on
// battery capacity and other factors, whereas MaxClockSpeed is a constant

uint speed = ReasonableClockSpeed;
using (var searcher =
new ManagementObjectSearcher("select CurrentClockSpeed from Win32_Processor"))
{
T value = default(T);
using var searcher = new ManagementObjectSearcher($"select {field} from {table}");
foreach (var item in searcher.Get())
{
value = (T)item[field];
speed = Convert.ToUInt32(item["CurrentClockSpeed"]);
item.Dispose();
}
return value;
}

// using this as a means of short-circuiting the Ensure methods for slower machines
// to speed up the display of the menus. CurrentClockSpeed will vary depending on
// battery capacity and other factors, whereas MaxClockSpeed is a constant
var speed = Query<uint>("CurrentClockSpeed", "Win32_Processor");
if (speed == 0) speed = ReasonableClockSpeed;

// returns total RAM across all physical slots; as KB so convert to bytes
var memory = Query<ulong>("MaxCapacityEx", "Win32_PhysicalMemoryArray") * 1024;
//var memory = Query<ulong>("MaxCapacityEx", "Win32_PhysicalMemoryArray") * 1024;
//var memory = Query<ulong>("Capacity", "Win32_PhysicalMemory") * 1024;

//var memory = Math.Ceiling(Query<double>(
// "*", "Win32_OperatingSystem", "TotalVisibleMemorySize") * 1024);

double memory = 0;
using (var searcher =
new ManagementObjectSearcher("select * from Win32_OperatingSystem"))
{
foreach (var item in searcher.Get())
{
memory = Convert.ToDouble(item["TotalVisibleMemorySize"]);
item.Dispose();
}
}

return (speed, memory);
}
Expand Down
2 changes: 1 addition & 1 deletion OneMore/Helpers/Extensions/IntExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal static class IntExtensions
{ "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };

// byte size names (only handles up to TB)
private static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB" };
private static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB" };


// convert int to alphabet string
Expand Down

0 comments on commit 366cd9c

Please sign in to comment.