14#if defined(__GNUC__) && defined(__linux__)
17#if defined(__APPLE__) && defined(__GNUC__)
18#include <mach/mach_init.h>
20#elif (defined(__FreeBSD__) || defined(__OpenBSD__))
21#include <sys/resource.h>
24#elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
38#if defined(__APPLE__) && defined(__GNUC__)
40 task_t task = MACH_PORT_NULL;
41 struct task_basic_info t_info;
42 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
44 if (KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO,
45 (task_info_t)&t_info, &t_info_count)) {
48 int64_t resident_memory = t_info.resident_size;
49 return resident_memory;
51#elif defined(__GNUC__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && \
52 !defined(__EMSCRIPTEN__) && !defined(_WIN32)
56 snprintf(buf,
sizeof(buf),
"/proc/%u/statm", (
unsigned)getpid());
57 FILE*
const pf = fopen(buf,
"r");
59 if (fscanf(pf,
"%u", &
size) != 1)
return 0;
62 return int64_t{1024} *
size;
64#elif (defined(__FreeBSD__) || defined(__OpenBSD__))
66 int who = RUSAGE_SELF;
68 getrusage(who, &rusage);
69 return (int64_t)(int64_t{1024} * rusage.ru_maxrss);
71#elif defined(_MSC_VER) || defined(__MINGW32__) || \
75 PROCESS_MEMORY_COUNTERS pmc;
76 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,
77 GetCurrentProcessId());
80 if (GetProcessMemoryInfo(hProcess, &pmc,
sizeof(pmc))) {
81 memory = pmc.WorkingSetSize;
83 CloseHandle(hProcess);
In SWIG mode, we don't want anything besides these top-level includes.
int64_t GetProcessMemoryUsage()
GetProcessMemoryUsage.