title: Does mmap or malloc allocate RAM?
body: <p>I know this is probably a stupid question but i've been looking for awhile and can't find a definitive answer. If I use <code>mmap</code> or <code>malloc</code> (in C, on a linux machine) does either one allocate space in RAM? For example, if I have 2GB of RAM and wanted to use all available RAM could I just use a <code>malloc/memset</code> combo, <code>mmap</code>, or is there another option I don't know of?</p>
  
  <p>I want to write a series of simple programs that can run simultaneously and keep all RAM used in the process to force swap to be used, and pages swapped in/out frequently. I tried this already with the program below, but it's not exactly what I want. It does allocate memory (RAM?), and force swap to be used (if enough instances are running), but when I call <code>sleep</code> doesn't that just lock the memory from being used (so nothing is actually being swapped in or out from other processes?), or am I misunderstanding something.</p>
  
  <p>For example, if I ran this 3 times would I be using 2GB (all) of RAM from the first two instances, and the third instance would then swap one of the previous two instances out (of RAM) and the current instance into RAM? Or would instance #3 just run using disk or virtual memory?</p>
  
  <p>This brings up another point, would I need to allocate enough memory to use all available virtual memory as well for the swap partition to be used?</p>
  
  <p>Lastly, would <code>mmap</code> (or any other C function. Hell, even another language if applicable) be better for doing this?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt; 
  
  #define MB(size) ( (size) * 1024 * 1024)
  #define GB(size) ( (size) * 1024 * 1024 * 1024)
  
  
  int main(){
      char *p;
      p = (char *)malloc(MB(512));
      memset(p, 'T', MB(512));
      printf(".5 GB allocated...\n");
  
      char *q;
      q = (char *)malloc(MB(512));
      memset(q, 'T', MB(512));
      printf("1 GB allocated...\n");
      printf("Sleeping...\n");
  
      sleep(300);
  }
  </code></pre>
  
  <p>** Edit: I am using CentOS 6.4 (with 3.6.0 kernel) for my OS, if that helps any.</p>