@aescling Stupid question: How to measure how much RAM a program uses?
@vaporeon_ that is hard to do well because purrograms commonly allocate way more memory than they actually ever use (idk how this really works tbh but the kernel does a lot of magic here). htop will give you reasonable enough answers. if your purrogram is being supervised by systemd, systemctl status will tell you current and peak memory usage but idk what exactly it is calculating
@aescling > idk how this really works tbh but the kernel does a lot of magic here
In a lecture that I attended, I was told that it uses something called "first touch policy", where only once the program actually accesses a page of memory, the kernel actually backs it by a page of physical memory (and for a system with NUMA and multiple cores, it'll choose to locate the page closest to the processor that touched it!)
For a practical test, I ran this program (after getting bored of trying to find the maximum value manually; the size of memory + swap on my system is 12'713'656'320 bytes):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
size_t i = 12713656300;
char *p;
while (p = malloc(i)) { free(p); i++; }
printf("%zu\n", i-1);
return 0;
}
And I can allocate up to MEM+SWAP-21 bytes before malloc() starts returning a NULL pointer!
If I sleep for 60 seconds with all that memory allocated, still, it doesn't affect my system at all, since I didn't actually touch that memory, so it doesn't need to be backed by a physical page
@vaporeon_ yeah i know, i’m just saying how i specifically know how much memory nginx uses on “my” servers