Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct macOS Free and Available Memory #906

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions drivers/unix/os_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,24 @@ Dictionary OS_Unix::get_memory_info() const {
ERR_PRINT(vformat("Could not get vm.swapusage, error code: %d - %s", errno, strerror(errno)));
}

// used memory: https://github.com/apple-opensource/top/blob/e7979606cf63270663a62cfe69f82d35cef9ba58/globalstats.c#L433
int64_t total_used_count = vmstat.wire_count + vmstat.inactive_count + vmstat.active_count + vmstat.compressor_page_count;
int64_t total_used = total_used_count * (int64_t)pagesize;
if (phy_mem != 0) {
meminfo["physical"] = phy_mem;
}
if (vmstat.free_count * (int64_t)pagesize != 0) {
meminfo["free"] = vmstat.free_count * (int64_t)pagesize;
}
if (swap_used.xsu_avail + vmstat.free_count * (int64_t)pagesize != 0) {
meminfo["available"] = swap_used.xsu_avail + vmstat.free_count * (int64_t)pagesize;
int64_t total_free = phy_mem - total_used;
if (total_free != 0) {
/*
** subtract used memory from total memory != vmstat.free_count: this can be observed from manual testing...
** correct value matches code from running macOS's open source top(1) utility; vmstat.free_count doesn't...
** https://github.com/apple-opensource/top/blob/e7979606cf63270663a62cfe69f82d35cef9ba58/globalstats.c#L433
** printf("incorrect value: %lld\ncorrect value: %lld\n", vmstat.free_count * pagesize, total_free); // try
*/
meminfo["free"] = total_free;
if (swap_used.xsu_avail + total_free != 0) {
meminfo["available"] = swap_used.xsu_avail + total_free;
}
}
}
#elif defined(__FreeBSD__)
int pagesize = 0;
Expand Down