Skip to content

Commit 5e6b782

Browse files
committed
mac mem
1 parent d5526bc commit 5e6b782

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/memory.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#include "sys/sysinfo.h"
66
#include <unistd.h>
77
#elif __APPLE__
8-
//#include mac hdr
8+
#include <mach/vm_statistics.h>
9+
#include <mach/mach_types.h>
10+
#include <mach/mach_init.h>
11+
#include <mach/mach_host.h>
912
#endif
1013

1114
// [[Rcpp::export(name = ".availableRAM")]]
@@ -21,9 +24,25 @@ double availableRAM(double ram) {
2124
sysinfo (&memInfo);
2225
ram = memInfo.freeram;
2326
#else
27+
vm_size_t page_size;
28+
mach_port_t mach_port;
29+
mach_msg_type_number_t count;
30+
vm_statistics64_data_t vm_stats;
31+
32+
mach_port = mach_host_self();
33+
count = sizeof(vm_stats) / sizeof(natural_t);
34+
if (KERN_SUCCESS == host_page_size(mach_port, &page_size) &&
35+
KERN_SUCCESS == host_statistics64(mach_port, HOST_VM_INFO,
36+
(host_info64_t)&vm_stats, &count)) {
37+
long long free_memory = (int64_t)vm_stats.free_count * (int64_t)page_size;
38+
39+
// long long used_memory = ((int64_t)vm_stats.active_count +
40+
// (int64_t)vm_stats.inactive_count +
41+
// (int64_t)vm_stats.wire_count) * (int64_t)page_size;
42+
ram = free_memory;
43+
}
2444
// mac
25-
// perhaps use this
26-
// https://stackoverflow.com/questions/38490320/how-to-query-amount-of-allocated-memory-on-linux-and-osx
45+
//https://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process
2746
#endif
2847

2948
return ram;

0 commit comments

Comments
 (0)