50 #elif defined ARCH_DARWIN
51 #include <mach/mach_host.h>
52 #include <mach/vm_statistics.h>
53 #include <sys/sysctl.h>
54 #include <sys/types.h>
59 unsigned long totalRamMB;
60 unsigned long freeRamMB;
61 unsigned long bufferRamMB;
64 void parseMeminfo(
int& total,
int& free,
int& buffer)
68 FILE* info = fopen(
"/proc/meminfo",
"r");
69 if(fscanf (info,
"MemTotal: %d kB MemFree: %d kB Buffers: %d kB Cached: %d kB",&total, &memfree, &buffer, &memcache) < 4)
72 info = fopen(
"/proc/meminfo",
"r");
74 if(fscanf(info,
"MemTotal: %d kB MemFree: %d kB MemAvailable: %d kB Buffers: %d kB Cached: %d kB",&total, &memfree, &free, &buffer, &memcache) < 5)
83 free = memfree + (buffer + memcache);
89 free = memfree + (buffer + memcache);
99 void MemoryInfoUpdate(MemoryVacancy & _outMemoryVacancy) {
102 _outMemoryVacancy.totalRamMB = 0;
103 _outMemoryVacancy.freeRamMB = 0;
104 _outMemoryVacancy.bufferRamMB = 0;
107 #ifdef WIN32 //Windows
111 ms.dwLength =
sizeof(ms);
113 GlobalMemoryStatusEx(&ms);
115 _outMemoryVacancy.totalRamMB = ms.ullTotalPhys / 1024 / 1024;
116 _outMemoryVacancy.freeRamMB = ms.ullAvailPhys / 1024 / 1024;
118 #elif defined ARCH_DARWIN // Apple (sadly cant query free memory)
119 mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
120 vm_statistics_data_t vmstat;
121 if(KERN_SUCCESS != host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat, &count))
128 uint64_t physical_memory;
132 length =
sizeof(uint64_t);
133 if( sysctl(mib, 2, &physical_memory, &length, NULL, 0) == -1 )
138 _outMemoryVacancy.totalRamMB = physical_memory / 1024 / 1024;
139 unsigned long active = vmstat.active_count * PAGE_SIZE / 1024 / 1024;
140 _outMemoryVacancy.freeRamMB = _outMemoryVacancy.totalRamMB - active;
141 _outMemoryVacancy.bufferRamMB =0;
146 int total, free, buffer;
147 parseMeminfo(total, free, buffer);
150 _outMemoryVacancy.totalRamMB = (long)total / 1024;
151 _outMemoryVacancy.freeRamMB = (long)free / 1024;
152 _outMemoryVacancy.bufferRamMB = (long)buffer / 1024;
156 unsigned long queryFreeRAM()
159 MemoryInfoUpdate(vac);
160 return vac.freeRamMB;
163 unsigned long queryTotalRAM()
166 MemoryInfoUpdate(vac);
167 return vac.totalRamMB;
170 unsigned long queryBufferedRAM()
173 MemoryInfoUpdate(vac);
174 return vac.bufferRamMB;