/// <summary> /// Gets the amount of available (free) memory on the system. /// </summary> /// <returns> /// Amount of available (free) memory on the system in bytes. /// </returns> public static ulong GetAvailableMemory() { uint hostInfoCount = HOST_VM_INFO_COUNT; var vmstat = new vm_statistics_data_t(); uint host_priv = mach_host_self(); PInvokeUtils.Try(() => host_statistics(host_priv, HostStatsType.HOST_VM_INFO, ref vmstat, ref hostInfoCount), result => result == KERN_SUCCESS); ulong physicalMemory = GetPhysicalMemory(); double pagesTotal = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count; double pagesFreePct = vmstat.free_count / pagesTotal; double bytesFree = pagesFreePct * physicalMemory; return((ulong)bytesFree); }
static extern int host_statistics( [In] uint host_priv, [In][MarshalAs(UnmanagedType.SysUInt)] HostStatsType flavor, [In, Out] ref vm_statistics_data_t host_info, [In, Out] ref uint host_info_count );