12345ieee
12345ieee

Reputation: 509

Program with smallest possible memory footprint (on Linux)

I am looking for a program that does the following things:

  1. Waits indefinitely, aka does not quit by itself when called with ./prog &
  2. Has the smallest possible memory footprint while running, as measured by, say, the pmap tool, program and [stack] fields.

This question isn't purely academic, I'm working on a memory scanner and I need a minimal example to work with.

The smallest I could come up with (in pure C) is:

#include <unistd.h>

int main(int argc, char **argv)
{
    pause();
    return 0;
}

but I'm sure this can be dwarfed with some assembly/compiler/C arcane magic, as this one eats over 180 KB of program+stack.

Upvotes: 1

Views: 265

Answers (1)

fuz
fuz

Reputation: 93172

Try this program:

#include <sys/syscall.h>

     .text
     .globl _start
     .type _start,@function
_start:
     mov $SYS_pause,%eax
     syscall # pause();
     ud2     # crash if pause() returns (should not happen)

Save in a file named pause.S (capital S) and assemble and link like this:

cc -c pause.S
ld -o pause pause.o

This shows as consuming one page of memory on my machine. This page is probably one page of stack space, as the text segment is mapped from the binary and thus does not consume RAM.

Upvotes: 6

Related Questions