1#if !defined(spp_memory_h_guard)
2#define spp_memory_h_guard
8#if defined(_WIN32) || defined( __CYGWIN__)
17#elif defined(__linux__)
18 #include <sys/types.h>
19 #include <sys/sysinfo.h>
20#elif defined(__FreeBSD__)
25 #include <sys/sysctl.h>
39 MEMORYSTATUSEX memInfo;
40 memInfo.dwLength =
sizeof(MEMORYSTATUSEX);
41 GlobalMemoryStatusEx(&memInfo);
42 return static_cast<uint64_t
>(memInfo.ullTotalPageFile);
43#elif defined(__linux__)
44 struct sysinfo memInfo;
46 auto totalVirtualMem = memInfo.totalram;
48 totalVirtualMem += memInfo.totalswap;
49 totalVirtualMem *= memInfo.mem_unit;
50 return static_cast<uint64_t
>(totalVirtualMem);
51#elif defined(__FreeBSD__)
54 size_t pageCntLen =
sizeof(pageCnt);
56 struct kvm_swap kswap;
57 uint64_t totalVirtualMem;
59 pageSize =
static_cast<u_int
>(getpagesize());
61 sysctlbyname(
"vm.stats.vm.v_page_count", &pageCnt, &pageCntLen, NULL, 0);
62 totalVirtualMem = pageCnt * pageSize;
64 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY,
"kvm_open");
65 kvm_getswapinfo(kd, &kswap, 1, 0);
67 totalVirtualMem += kswap.ksw_total * pageSize;
69 return totalVirtualMem;
78 MEMORYSTATUSEX memInfo;
79 memInfo.dwLength =
sizeof(MEMORYSTATUSEX);
80 GlobalMemoryStatusEx(&memInfo);
81 return static_cast<uint64_t
>(memInfo.ullTotalPageFile - memInfo.ullAvailPageFile);
82#elif defined(__linux__)
83 struct sysinfo memInfo;
85 auto virtualMemUsed = memInfo.totalram - memInfo.freeram;
87 virtualMemUsed += memInfo.totalswap - memInfo.freeswap;
88 virtualMemUsed *= memInfo.mem_unit;
90 return static_cast<uint64_t
>(virtualMemUsed);
91#elif defined(__FreeBSD__)
94 u_int pageCnt, freeCnt;
95 size_t pageCntLen =
sizeof(pageCnt);
96 size_t freeCntLen =
sizeof(freeCnt);
97 struct kvm_swap kswap;
98 uint64_t virtualMemUsed;
100 pageSize =
static_cast<u_int
>(getpagesize());
102 sysctlbyname(
"vm.stats.vm.v_page_count", &pageCnt, &pageCntLen, NULL, 0);
103 sysctlbyname(
"vm.stats.vm.v_free_count", &freeCnt, &freeCntLen, NULL, 0);
104 virtualMemUsed = (pageCnt - freeCnt) * pageSize;
106 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY,
"kvm_open");
107 kvm_getswapinfo(kd, &kswap, 1, 0);
109 virtualMemUsed += kswap.ksw_used * pageSize;
111 return virtualMemUsed;
120 PROCESS_MEMORY_COUNTERS_EX pmc;
121 GetProcessMemoryInfo(GetCurrentProcess(),
reinterpret_cast<PPROCESS_MEMORY_COUNTERS
>(&pmc),
sizeof(pmc));
122 return static_cast<uint64_t
>(pmc.PrivateUsage);
123#elif defined(__linux__)
127 auto i = strlen(line);
129 while(*line <
'0' || *line >
'9')
139 auto file = fopen(
"/proc/self/status",
"r");
143 while(fgets(line, 128, file) !=
nullptr)
145 if(strncmp(line,
"VmSize:", 7) == 0)
147 result = parseLine(line);
153 return static_cast<uint64_t
>(result) * 1024;
154#elif defined(__FreeBSD__)
155 struct kinfo_proc info;
156 size_t infoLen =
sizeof(info);
157 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
159 sysctl(mib,
sizeof(mib) /
sizeof(*mib), &info, &infoLen, NULL, 0);
160 return static_cast<uint64_t
>(info.ki_rssize * getpagesize());
169 MEMORYSTATUSEX memInfo;
170 memInfo.dwLength =
sizeof(MEMORYSTATUSEX);
171 GlobalMemoryStatusEx(&memInfo);
172 return static_cast<uint64_t
>(memInfo.ullTotalPhys);
173#elif defined(__linux__)
174 struct sysinfo memInfo;
177 auto totalPhysMem = memInfo.totalram;
179 totalPhysMem *= memInfo.mem_unit;
180 return static_cast<uint64_t
>(totalPhysMem);
181#elif defined(__FreeBSD__)
183 size_t physMemLen =
sizeof(physMem);
184 int mib[] = { CTL_HW, HW_PHYSMEM };
186 sysctl(mib,
sizeof(mib) /
sizeof(*mib), &physMem, &physMemLen, NULL, 0);
uint64_t GetTotalMemoryUsed()
Definition: meminfo.h:75
uint64_t GetSystemMemory()
Definition: meminfo.h:36
uint64_t GetProcessMemoryUsed()
Definition: meminfo.h:117
uint64_t GetPhysicalMemory()
Definition: meminfo.h:166