diff --git a/cli/tests/unit_node/os_test.ts b/cli/tests/unit_node/os_test.ts
index 2d0d3a8c80..54dc375bbf 100644
--- a/cli/tests/unit_node/os_test.ts
+++ b/cli/tests/unit_node/os_test.ts
@@ -289,3 +289,23 @@ Deno.test({
await child.status;
},
});
+
+Deno.test({
+ name:
+ "os.freemem() is equivalent of Deno.systemMemoryInfo().free except on linux",
+ ignore: Deno.build.os === "linux",
+ fn() {
+ const diff = Math.abs(os.freemem() - Deno.systemMemoryInfo().free);
+ assert(diff < 10_000);
+ },
+});
+
+Deno.test({
+ name:
+ "os.freemem() is equivalent of Deno.systemMemoryInfo().available on linux",
+ ignore: Deno.build.os !== "linux",
+ fn() {
+ const diff = Math.abs(os.freemem() - Deno.systemMemoryInfo().available);
+ assert(diff < 10_000);
+ },
+});
diff --git a/ext/node/polyfills/os.ts b/ext/node/polyfills/os.ts
index 06de4ad444..d1f5e7bfae 100644
--- a/ext/node/polyfills/os.ts
+++ b/ext/node/polyfills/os.ts
@@ -159,7 +159,14 @@ export function endianness(): "BE" | "LE" {
/** Return free memory amount */
export function freemem(): number {
- return Deno.systemMemoryInfo().free;
+ if (Deno.build.os === "linux") {
+ // On linux, use 'available' memory
+ // https://github.com/libuv/libuv/blob/a5c01d4de3695e9d9da34cfd643b5ff0ba582ea7/src/unix/linux.c#L2064
+ return Deno.systemMemoryInfo().available;
+ } else {
+ // Use 'free' memory on other platforms
+ return Deno.systemMemoryInfo().free;
+ }
}
/** Not yet implemented */
diff --git a/runtime/ops/os/README.md b/runtime/ops/os/README.md
index 837bb7b3cd..ae1a5958e4 100644
--- a/runtime/ops/os/README.md
+++ b/runtime/ops/os/README.md
@@ -27,6 +27,6 @@
| Target family | Syscall | Description |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
-| Linux | sysinfo | - |
+| Linux | sysinfo and `/proc/meminfo` | - |
| Windows | `sysinfoapi::GlobalMemoryStatusEx` | - |
| macOS |
sysctl([CTL_HW, HW_MEMSIZE]);| - | diff --git a/runtime/ops/os/sys_info.rs b/runtime/ops/os/sys_info.rs index d8175f31bf..b3d2cd743c 100644 --- a/runtime/ops/os/sys_info.rs +++ b/runtime/ops/os/sys_info.rs @@ -211,8 +211,20 @@ pub fn mem_info() -> Option
sysctl([CTL_VM, VM_SWAPUSAGE]);
host_statistics64(mach_host_self(), HOST_VM_INFO64)