Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

No, because the pages aren't mapped to anything -- not disk, RAM, or an external paging device. Allocating a chunk of memory typically doesn't map those pages to anything immediately (since there's no benefit to doing that extra work).

The current Linux man page gives a bit more insight:

   mlockall() and munlockall()
       mlockall()  locks   all   pages
       mapped  into  the address space
       of the calling  process.   This
       includes the pages of the code,
       data and stack segment, as well
       as shared libraries, user space
       kernel data, shared memory, and
       memory-mapped    files.     All
       mapped pages are guaranteed  to
       be  resident  in  RAM  when the
       call returns successfully;  the
       pages are guaranteed to stay in
       RAM until later unlocked.

       The  flags  argument  is   con‐
       structed  as  the bitwise OR of
       one or more  of  the  following
       constants:

       MCL_CURRENT Lock    all   pages
                   which are currently
                   mapped   into   the
                   address  space   of
                   the process.

       MCL_FUTURE  Lock    all   pages
                   which  will  become
                   mapped   into   the
                   address  space   of
                   the  process in the
                   future.       These
                   could    be,    for
                   instance, new pages
                   required by a grow‐
                   ing heap and  stack
                   as well as new mem‐
                   ory-mapped files or
                   shared       memory
                   regions.


Ah, I see--it's all mapped pages, not all pages as irishsultan's comment says. That makes a lot more sense, thanks!


After some experimenting it will fault all the mapped pages, causing the VM subsystem to try to back them. In Linux 4.4+ there is an a flag to `mlockall(2)` called "MCL_ONFAULT" which does not do that and instead locks the pages as they become backed.

    $ cat wheres-the-ram.c
    #! /home/rkeene/bin/c
    #include <sys/mman.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char **argv) {
            unsigned char *buffer;
            int mla_ret;
    
            mla_ret = mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT);
            if (mla_ret != 0) {
                    perror("mlockall");
    
                    return(1);
            }
    
            buffer = malloc(1024LLU * 1024LLU * 1024LLU * 32LLU);
            if (!buffer) {
                    perror("malloc");
    
                    return(1);
            }
    
            buffer[0] = 1;
            buffer[1] = buffer[0];
            buffer[2] = buffer[3];
    
            puts("Success !");
    
            return(0);
    }
    $ sudo ./wheres-the-ram.c
    Success !
    $ free -g
                  total        used        free      shared  buff/cache   available
    Mem:             14           1           5           0           7          13
    Swap:             0           0           0
    $


Never seen someone "execute C" before. You are never done learning apparently obvious things...


The canonical shebang line to do this is '/usr/bin/tcc -r' - the Tiny C Compiler in "run" mode.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: