Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:06:38+00:00 2026-06-02T06:06:38+00:00

I am experimenting with mmap and came with the following sample code: int main()

  • 0

I am experimenting with mmap and came with the following sample code:

    int main() {

    int fd;
    char *filename = "/home/manu/file";
    struct stat statbuf;
    int i = 0;
    char c = *(filename);

    // Get file descriptor and file length
    fd = open(filename, O_RDONLY);
    if (fd == -1) {
        perror("fopen error");
    }
    if (fstat(fd, &statbuf) < 0) {
        perror("fstat error");
    }
    printf("File size is %ld\n", statbuf.st_size);

    // Map the file
    char* mmapA = (char*) mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE,
            fd, 0);
    if (mmapA == MAP_FAILED) {
        perror("mmap error");
        return 1;
    }

    // Touch all the mapped pages
    while (i < statbuf.st_size) {
        c = mmapA[i];
        i++;
    }
    c++;

    // Close file descriptor
    if (close(fd) == -1) {
        perror("close");
        return 1;
    }

    //Unmap file
    munmap(mmapA, statbuf.st_size);

    return EXIT_SUCCESS;
}

The file size is 137948 bytes = 134,7 kilobytes.
To inspect program’s memory I am using top, mainly the RES and VIRT columns. I am looking for these values at three different places:

  1. just before the mmap call
  2. just after the mmap call
  3. after having read all the mapped memory to have the file’s effectively loaded into main memory (after page faults)

The value reported by top are

  1. VIRT = 1828 RES = 244
  2. VIRT = 1964 RES = 248
  3. VIRT = 1964 RES = 508

1964 – 1828 = 136, I guess in kilobytes and thus perfectly match the file’s size.

But I can’t understand the RES difference of 508 – 248 = 260 .. Why is it different from virtual memory size and file size ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:06:41+00:00Added an answer on June 2, 2026 at 6:06 am

    One thing is certain: the results depend on the state of the system and not only on the running application. On my machine, the increase in RES was 136 kB the first two times I run the program, but the subsequent runs didn’t involve any increase at all – probably the OS already had the whole file in cache. Interestingly, the values themeselves differed significantly between runs. In the first run the jump in RES was from 344 to 480 kB, but the latter runs had a RES value of 348 kB all the time. There was a similar change in SHR: a jump of 136 kB first time and no change later.

    I was able to force the original case (with the 136 kB jump) at will by overwriting the file which is later mapped with with zeros using dd before running the app.

    I looked at pmaps output but it was exactly the same in both cases and didn’t change after the call to mmap().

    I can’t reproduce the oversized RES jump here, but here’s what you can do. Suppose your binary is compiled as a.out. Insert a 10 second sleep right after the mmap() and another 10 second sleep just before munmap(). This gives a time window to dump interesting information. We will read from /proc which exactly files are resident in memory. In order to do this, open up two tabs in your terminal, in one run

    ./a.out
    

    and then immediately in the other tab:

    for ((i=0;i<4;i++)); do cat /proc/$(ps -fe | egrep '[a]\.out' | awk '{print $2}')/smaps > smaps.$i; sleep 5; done
    

    This will create 4 snapshots of the program’s map states in four separate files. The difference between one of the consecutively numbered snapshot should show what changes during the surge in RES size. On my machine during a sample run, the difference was between snapshots 1 and 2, and the change was [note I changed the name of mapped file but it’s not important here]:

    user@machine:~$ diff -u smaps.{1,2}
    --- smaps.1     2012-04-19 00:01:46.000000000 +0200
    +++ smaps.2     2012-04-19 00:01:51.000000000 +0200
    @@ -84,13 +84,13 @@
     MMUPageSize:           4 kB
     b782f000-b7851000 r--p 00000000 08:05 429102     /tmp/tempfile
     Size:                136 kB
    -Rss:                   0 kB
    -Pss:                   0 kB
    +Rss:                 136 kB
    +Pss:                 136 kB
     Shared_Clean:          0 kB
     Shared_Dirty:          0 kB
    -Private_Clean:         0 kB
    +Private_Clean:       136 kB
     Private_Dirty:         0 kB
    -Referenced:            0 kB
    +Referenced:          136 kB
     Swap:                  0 kB
     KernelPageSize:        4 kB
     MMUPageSize:           4 kB
    

    What happens is exactly what should: the mapped file is initially not resident at all and 136 kB are resident later on.

    On your system, the diff should lead you to the source of the additional change in RES – you should be able to find out the name of the other file(s) whose Rss value changes. Some entries are not files, but other memory areas, for example you may find markers such as [heap] and [stack]. This should also prove or disprove nos’ suggestion about system libraries being loaded and stack usage growing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im experimenting with the following code private void timer1_Tick(object sender, EventArgs e) { Thread
While experimenting a bit with C++ templates I managed to produce this simple code,
I experimenting with Flex Styling, and I came across an alignment issue. I have
Just experimenting with different inheritance techniques in JS, and came across something mildly discomfiting
im experimenting in getting camel to do some file operations and pass them through
In experimenting with pickle, I've put together some code for a (very) simple blog.
I was experimenting with Java reflection and inlined Strings and came up with the
I'm doing some experimenting to try and get the file size smaller and I
I'm writing a small tool for experimenting with ELF-64 object code which is intended
Experimenting using different approaches and reducing code maintenance I have ended up using reflection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.