diff --git a/src/coreclr/gc/unix/cgroup.cpp b/src/coreclr/gc/unix/cgroup.cpp index ff1712235a339d..4abbcfa942dc3a 100644 --- a/src/coreclr/gc/unix/cgroup.cpp +++ b/src/coreclr/gc/unix/cgroup.cpp @@ -510,6 +510,9 @@ class CGroup static bool GetCGroupMemoryUsage(size_t *val, const char *filename, const char *inactiveFileFieldName) { + if (s_memory_cgroup_path == nullptr) + return false; + // Use the same way to calculate memory load as popular container tools (Docker, Kubernetes, Containerd etc.) // For cgroup v1: value of 'memory.usage_in_bytes' minus 'total_inactive_file' value of 'memory.stat' // For cgroup v2: value of 'memory.current' minus 'inactive_file' value of 'memory.stat' @@ -534,9 +537,6 @@ class CGroup if (!result) return result; - if (s_memory_cgroup_path == nullptr) - return false; - uint64_t inactiveFileValue = 0; if (GetCGroupMemoryStatField(inactiveFileFieldName, &inactiveFileValue)) { diff --git a/src/mono/mono/utils/mono-cgroup.c b/src/mono/mono/utils/mono-cgroup.c index 4c58f53257b97e..9a1fab301a2dc8 100644 --- a/src/mono/mono/utils/mono-cgroup.c +++ b/src/mono/mono/utils/mono-cgroup.c @@ -535,6 +535,9 @@ getCGroupMemoryLimit(size_t *val, const char *filename) static gboolean getCGroupMemoryUsage(size_t *val, const char *filename, const char *inactiveFileFieldName) { + if (s_memory_cgroup_path == NULL) + return FALSE; + /* * Use the same way to calculate memory load as popular container tools (Docker, Kubernetes, Containerd etc.) * For cgroup v1: value of 'memory.usage_in_bytes' minus 'total_inactive_file' value of 'memory.stat' @@ -561,9 +564,6 @@ getCGroupMemoryUsage(size_t *val, const char *filename, const char *inactiveFile if (!result) return result; - if (s_memory_cgroup_path == NULL) - return FALSE; - char *stat_filename = NULL; if (asprintf (&stat_filename, "%s%s", s_memory_cgroup_path, CGROUP_MEMORY_STAT_FILENAME) < 0) return FALSE;