-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
proposal: runtime: a hard limit version of GOMEMLIMIT for testing #63338
Comments
How would this work with cgo? |
On linux you can use cgroups of other features to limit how much memory a process can use. This includes a whole bunch of other things that the go runtime does not track (cgo as pointed out by rittneje, ... some network buffers if I'm not incorrect). Quick google searches suggest to me you can do the same thing on MacOS, Windows and Freebsd (given cgroups are linux specific). |
cc @golang/runtime @mknyszek |
As written, this should be a proposal, as it proposes a new environment variable and presumably a new API in |
Hm, I don't think this can work quite like The only way I could see this working is if it exists completely outside of At which point it seems like one could implement something similar themselves by having a background goroutine that watches the same statistics I also wouldn't recommend relying on
|
I would like this but not “for testing” but to make large allocations fail gracefully if they would go significantly over the limit, maybe I should file a different issue for that then or stay on this one? (today you can limit to X and yet ask 10X in a single alloc and it just continues (until the OS kills the go program or large alloc fatals) |
I filled mine under separetely under #68934 as I think that's easier to solve than overall death spiral GC |
What version of Go are you using (
go version
)?1.21, etc
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?More of an issue on MacOS, but exists anywhere I guess.
What did you do?
Tried to limit memory usage. Specifically, I wanted the allocator to fail (yes, I know this panics and can't be
recover
ed) rather than more memory to be used.What did you expect to see?
I don't honestly know. Memory usage is poorly-defined and inconsistent at the best of times.
What did you see instead?
So, modern MacOS just doesn't let you do anything much like
ulimit
anymore for memory usage. And modern Linux pretty much always does overcommit, and then if too much memory gets used, it kills... Something. Arbitrarily. Which may or may not be the thing you wanted.I'm looking at some code which has an unbounded memory usage problem, which I want to fix, and first I wanted to write a test case that would confirm that it was failing. And discovered that I can't sanely do this; I can't actually make the test fail due to excessive memory usage, because the amount of memory I can allocate (way over 60GB) takes long enough to populate that tests time out, and anyway a test that takes over ten minutes to run isn't a great fit for ever getting anything done.
And we have
GOMEMLIMIT
, but that's advisory. And what I sort of want is, roughly, aGOMEMLIMITHARD
, where when runtime is about to request more memory from the OS, it checks whether doing so would cause the runtime's total memory-requested-from-OS to exceed the limit, and if it would, to fail as though a request from the OS was denied.FAQ
Q: But that will just kill your program!
A: Yes, exactly. It will kill this program, rather than causing the kernel to pick some arbitrary program to kill. That is, in fact, highly preferable.
Q: But you can just use the OS's limits!
A: Apparently not on MacOS in the last few years (?!?!). Still seems to work on Linux via
ulimit -v
, although it's a bit approximate, because memory "usage" is not well-defined. (For instance, mmapped files sometimes but not always count as some kinds of memory.)The text was updated successfully, but these errors were encountered: