Conversation

en.cppreference.com/w/cpp/memory/n is a decent approach. An implementation as an example: github.com/GrapheneOS/har. It doesn't work well in C++ because it's only for new and people don't use it whenever they call malloc, mmap, etc. but it's an approach that can work well for the common case.
2
So, for example, modern malloc implementations can usually drop cached pages via MADV_DONTNEED in a memory exhaustion situation. In normal usage, they would generally use MADV_FREE. The same thing applies to many other allocators and caches. Android & Windows have APIs for this.
1
1
Chromium, Firefox, etc. use this and it's just stubbed out for non-Android Linux since there's nothing to use. MADV_FREE is much nicer for a performance-oriented malloc though, because when you wouldn't want to lock in the remaining faulted pages when you hand out allocations.
1
1
So, you do need some kind of system for handling allocation failure by initially asking allocators to purge their caches, even with an API like that, but for many other volatile caches it makes sense to use that approach, like a browser's in-memory cache and they do use it a bit.
1
1
Show replies