Is Sys.free_memory() "reliable"?

I’m trying to add a custom error message for a package if the user inputs data such that the memory of the system will explode. For such, I use:

if required_memory > 0.8 * Sys.free_memory()
    throw(.... appropriate error message... )

The problem is that after adding this to the package, CI tests started to fail, because in MacOS with Julia 1.9, it seems that free_memory() is returning way too small values. For instance, the error message says something like free_memory being 0.1Mb.

Those tests are small and do not fail normally, so it is not the case that that is the true free memory of the node running the CI test.

Are there any known issues associated to Sys.free_memory(), particularly in MacOS?

Ps: The error occurred in Julia 1.9 but not in 1.10.4, thus it might be a fixed bug somewhere in between.

Anyway I would like to know if that use of free_memory is reasonable one (I don’t want my users getting Killed for no apparent reason).

1 Like

I re-run the CI job that failed, and now it passed. I’m not sure if I’m happy or not.

(I wouldn’t like the users getting the error message randomly either…)

I’m definitely not an expert on this, but I think there’s a difference between “free memory” and “available memory”, where the latter includes OS caches which can be quickly freed. I think “free memory” is kinda wasted (since it could be caching something, as long as the OS can quickly dump the cache when the memory is needed for something else), and MacOS tends to not have much of it. And Julia doesn’t seem to expose a Sys.available_memory() or similar.


If Mac owners can confirm that Sys.free_memory() is generally small and not ver related to available memory, I’d be grateful.

For me, I am on MacOS and get

julia> Base.format_bytes(Sys.free_memory())
"68.375 MiB"

while activity monitor says “memory used” is 2 gb less than “physical memory”:

so I guess I am using a large % of my memory but have available more than the tiny bit Sys.free_memory() says. I for one would be disappointed if a Julia package refused to allocate more than that to me; I can allocate it without an issue or noticeable slowdown:

julia> Base.format_bytes(Base.summarysize(rand(10_000_000)))
"76.294 MiB"
1 Like

I am on Linux (Ubuntu) and for me Sys.free_memory seems to count memory used for cache as “free”:

julia> Sys.free_memory() |> Base.format_bytes
"10.072 GiB"

htop output (green - used, orange - cache)

Thanks for the feedback. I decided then to issue a warning if the memory exceeds a fraction of the total memory, and only error if the required memory is larger than the total memory.