Jeremy
Jeremy

Reputation: 3

C malloc() arrays aren't taking up memory even after being accessed

I've been running into a problem trying to show the amount of memory used up by dynamically allocated arrays using the linux tool top through a system() call in my program.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int Array_Length = getpagesize();
    int pid = getpid();
    char top_call[22];
    sprintf(top_call, "top -b -n1 -p %d", pid);

    int *Array1 = malloc(Array_Length * sizeof(int));
    int *Array2 = malloc(Array_Length * sizeof(int));
    system(top_call);

    int i;
    for(i = 0; i < Array_Length; i++) {
        Array1[i] = 1;
        Array2[i] = 1;
    }
    system(top_call);

    return 0;
}

First system(top_call) results were as I expected:

top - 18:30:01 up 5:05, 2 users, load average: 0.00, 0.01, 0.04  
Tasks: 1 total, 0 running, 0 sleeping, 0 stopped, 0 zombie  
Cpu(s): 0.3%us, 0.1%sy, 0.0%ni, 99.5%id, 0.1%wa, 0.0%hi, 0.0%si, 0.0%st  
Mem:  249852k total, 236660k used,  13192k free, 66180k buffers  
Swap: 407548k total,    128k used, 407420k free, 72952k cached  

 PID  VIRT RES SHR %MEM SWAP CODE DATA  
2000 10280 564 464  0.2 9716    8 8512

Since malloc() uses lazy allocation, after the second system(top_call) I was expecting the memory usage to increase since the arrays have now been accessed. However, the results after the second call show the same amount of memory usage from my process:

top - 18:41:22 up 5:16, 2 users, load average: 0.00, 0.01, 0.04  
Tasks: 1 total, 0 running, 0 sleeping, 0 stopped, 0 zombie  
Cpu(s): 0.3%us, 0.1%sy, 0.0%ni, 99.5%id, 0.1%wa, 0.0%hi, 0.0%si, 0.0%st  
Mem:  249852k total, 236052k used,  13800k free, 66236k buffers  
Swap: 407548k total,    128k used, 407420k free, 72948k cached  

 PID  VIRT RES SHR %MEM SWAP CODE DATA  
2000 10280 564 464  0.2 9716    8 8512

Can anyone explain why my arrays aren't using up any memory?

Apologies if the formatting isn't that great.

Upvotes: 0

Views: 137

Answers (2)

Jose Rey
Jose Rey

Reputation: 251

Arrays consuming memory:

top - 18:56:01 up 76 days,  3:40,  1 user,  load average: 0.02, 0.03, 0.06
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.9%us,  0.2%sy,  0.0%ni, 98.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2956544k total,  2155692k used,   800852k free,   218780k buffers
Swap:  4194296k total,   433716k used,  3760580k free,   732372k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
20772 aluser    20   0  3916  444  368 S  0.0  0.0   0:00.00 a.out

top - 18:56:01 up 76 days,  3:40,  1 user,  load average: 0.02, 0.03, 0.06
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.9%us,  0.2%sy,  0.0%ni, 98.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2956544k total,  2155940k used,   800604k free,   218780k buffers
Swap:  4194296k total,   433716k used,  3760580k free,   732372k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
20772 aluser    20   0 35924  476  392 S  0.0  0.0   0:00.00 a.out

top - 18:56:02 up 76 days,  3:40,  1 user,  load average: 0.02, 0.03, 0.06
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.9%us,  0.2%sy,  0.0%ni, 98.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2956544k total,  2188708k used,   767836k free,   218780k buffers
Swap:  4194296k total,   433716k used,  3760580k free,   732372k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
20772 aluser    20   0 35924  31m  392 S  0.0  1.1   0:00.04 a.out

There are 3 calls to top, because malloc is not being lazy, I've also allocated 1000 times more memory to make figures more evident and force the request of memory from OS. Here is the modified program:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int Array_Length = getpagesize() * 1000;
    int pid = getpid();
    char top_call[22];
    sprintf(top_call, "top -b -n1 -p %d", pid);

    system(top_call);
    int *Array1 = malloc(Array_Length * sizeof(int));
    int *Array2 = malloc(Array_Length * sizeof(int));
    system(top_call);

    int i;
    for(i = 0; i < Array_Length; i++) {
        Array1[i] = 1;
        Array2[i] = 1;
    }
    system(top_call);

    return 0;
}

Upvotes: 1

gnasher729
gnasher729

Reputation: 52538

You seem to be assuming that every call to malloc () will ask the operating system to allocate memory. That's very unlikely, especially for small allocations. You should expect allocations from the OS to be very, very rare.

Upvotes: 2

Related Questions