From 0fe5722744318069da8c0a3b7e2cd7bac3954ef3 Mon Sep 17 00:00:00 2001 From: Lixin Wei Date: Fri, 14 Aug 2020 14:47:52 +0800 Subject: [PATCH] [Core] Add cached memory to unsued memory in Linux/BSD (#10084) * add cached memory to available memory * format * bug fixed * bug fixed * fixed * lint --- python/ray/memory_monitor.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/python/ray/memory_monitor.py b/python/ray/memory_monitor.py index c460b22d5..9f52dc725 100644 --- a/python/ray/memory_monitor.py +++ b/python/ray/memory_monitor.py @@ -108,8 +108,15 @@ class MemoryMonitor: return # escape hatch, not intended for user use self.last_checked = time.time() - total_gb = psutil.virtual_memory().total / (1024**3) - used_gb = total_gb - psutil.virtual_memory().available / (1024**3) + psutil_mem = psutil.virtual_memory() + total_gb = psutil_mem.total / (1024**3) + used_gb = total_gb - psutil_mem.available / (1024**3) + + # Linux, BSD has cached memory, which should + # also be considered as unused memory + if hasattr(psutil_mem, "cached"): + used_gb -= psutil_mem.cached / (1024**3) + if self.cgroup_memory_limit_gb < total_gb: total_gb = self.cgroup_memory_limit_gb with open("/sys/fs/cgroup/memory/memory.usage_in_bytes",