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

Include buffer/cache memory as free memory on linux #9

Closed

Conversation

dustin-decker
Copy link

The Linux kernel will typically use almost all free memory for buffering/cache purposes such as the page cache, freeing immediately as needed for applications. This buffer should be considered free memory.

@pbnjay
Copy link
Owner

pbnjay commented Mar 10, 2022

I know that BSD + ZFS has similar filesystem caching going on, we could make a similar change there.
I'll have to read about darwin's page types in vm_stat to see what may be possible there (inactive/speculative/purgeable/etc all sound viable)

I had debated this in the initial implementation, but hesitated because the primary use is to allow "dynamic code to minimize thrashing and other contention." I don't have any benchmarks to back up the possible contention from filling available memory with application data in a read-heavy app, which is why I hesitate to include it directly. Do you have any ideas on how to validate that? At the very least I think it should be mentioned in the documentation.

I think a simple solution to both challenges could be to feature gate the inclusion with some sort of explicit opt-in and explanation. e.g. you'd have to call memory.IncludeCaches(enabled bool) or similar first.

@dustin-decker
Copy link
Author

In my application, we throttle down some concurrency when there is memory pressure, which we've defined as <2GB free. On a machine with 16GB of memory we had ~11GB of buffer/cache (primarily page cache) but only ~500mb actually free.

This certainly would be a breaking change for the library.

What about introducing a BufferMemoryUsed() function?

@dustin-decker
Copy link
Author

Actually, I think it may be best to add a AvailableMemory() function that includes free+buffer.

https://haydenjames.io/free-vs-available-memory-in-linux/#:~:text=The%20difference%20between%20free%20memory,penalty%20of%20using%20swap%20space.

@dustin-decker dustin-decker mentioned this pull request Mar 11, 2022
4 tasks
@dustin-decker
Copy link
Author

Closing this in favor of #10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants