Question: I am quite confused how memory allocation (malloc/calloc) works in linux/c. Suppose, I have a machine with 16GB RAM and I run a program as root. It is 64-bit machine, so all 16GB can be addressed as a single segment. Can I allocate all of that (less the amount for OS of course) with a single malloc call? with many malloc calls?

How it all relates to "heap memory" and "virtual memory"? If i allocate a small memory chunk, and it happens be in heap memory, then I resize (enlarge) this chunk, what happens when I approach stack area?

Do I have to fiddle with setrlimit RLIMIT\_AS if I want (almost) all RAM to be assigned to my single process, even thought it's running as root?

Answer: 
On a virtual memory OS (such as Linux) malloc() does not allocate *memory*. It allocates *address space* For instance, compile and run the following fragment, and (in another terminal) run `top`:

```
#include <stdlib.h>
#include <unistd.h>

int main(void) {
  char *cp;
  cp = malloc( 16ULL * 1024 *1024 *1024);
  if (cp) pause();
  return 0;
}

```

At my computer, TOP displays:

```
PID    USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
29026 plasser    20   0 16.0g  324  248 S    0  0.0   0:00.00 a.out 

```

Which means: a.out has 16GB Virtual size, and uses only 324 (bytes? KB?) resident memory (probably just the binary)

What happenened?

* the malloc() call has asked the OS to extend the **address space**, adding 16 GB
* The OS has fulfilled this request, and set up pagetables for it et cetera.
* these page tables have no physical memory *attached* to them (yet), maybe except for the tables themselves
* once the program starts referencing this address space, pages will be attached (The OS wil *fault them in*)
* (the pages will probably be COW'ed from /dev/zero, but that is only a detail)
* but, at the moment the program does reference the addresses, memory will have to be be allocated by the OS and become *attached* to the process. (and it will show up in the `RES` field, Trust me )
* at some moment, the *attached* memory could be detached too, once the OS thinks it has not been used for a long time. It will be added to a pool of free memory or/and used for other purposes. Its contents may be pushed to *backing store* (disk) in this case

`
A 64-bit process can allocate all of the memory. It doesn't even need to be root, unless the system has defined a `ulimit` setting for non-root users. Try `ulimit -v` to see if a limit is set.

Under Linux default settings a process can ask for nearly any amount of memory and it will be granted. The memory will be actually assigned as it is used, and it will come from physical RAM or from disk swap as needed.

A memory allocation resize is normally done in the C library by allocating the new, larger size and copying the old data into the new allocation. It is not usually done by expanding the existing allocation. Memory allocations are chosen to not conflict with other allocations such as program stack.