google / tcmalloc

Apache License 2.0
4.41k stars 480 forks source link

Add callback for the soft memory limit handling #270

Open abyss7 opened 1 week ago

abyss7 commented 1 week ago

This feature is useful i.e. to handle large heap allocations, pre-OOM state, or just signal about a certain size of heap.

ckennelly commented 4 days ago

Thanks for the pull request.

It's somewhat tricky to support a callback right here, since we're holding pageheap_lock. We read stats about memory usage under that lock, and that plausibly seems like something a zero-argument callee might need to do.

For our internal use cases that need to track memory usage, we've had a lot of success with using callbacks on our sampled allocations: It allows the application to track memory usage with about 2MB of error either way and can be done without holding any locks. While we haven't (yet) open-sourced these APIs, is that something that would work for your use case?