We'll also demonstrate in the following section a more hands-on and interesting way by which you can (most probably) invite the OOM killer in. Write a simple user space C program that behaves as a crazy allocator, performing (typically) tens of thousands of memory allocations, writing something to each page, and, of course, never freeing up the memory, thus putting tremendous pressure on memory resources.
As usual, we show only the most relevant parts of the source code in the following snippet; please refer to and clone the book's GitHub repo for the full code; remember, this is a user-mode app not a kernel module:
// ch9/oom_killer_try/oom_killer_try.c
#define BLK (getpagesize()*2)
static int force_page_fault = 0;
int main(int argc, char **argv)
{
char *p;
int i = 0, j = 1, stepval = 5000, verbose = 0;
[...]
do {
p = (char *)malloc(BLK);
if (!p) {
fprintf(stderr, "%s: loop #%d: malloc failure...