3

I wrote a program to calculate the time it takes to allocate memory for a malloc function.

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

int main() {
    FILE *f;
    f = fopen("malloc.txt","w");
    int size = 1024*1024;
    for (int i = 1; i <= 1024; i++) {
        int *a;
        clock_t begin = clock();
        a = (int*)malloc(i*size);
        clock_t end = clock();
        free(a);
        double time = (end - begin) / CLOCKS_PER_SEC;
        fprintf(f,"%d %f\n",i, time);
    }
    fclose(f);
}

I then plot the allocation time dependency against the size of the memory to be allocatedenter image description here

can you explain to me why the graph is not increasing linearly but barely increasing and there are sudden spikes?

Ivan Ivan
  • 49
  • 3
  • 1
    `malloc` doesn't initialize the memory it returns so the size of the block requested doesn't increase the time to allocate it. If you were using `calloc` it might make a difference. – Retired Ninja May 04 '22 at 19:33
  • 2
    With virtual memory the system can defer the actual allocation of storage until the program actually uses the storage. Think of it as space blocked off in virtual memory. When the program writes to or reads from that space, the system will find and assign some RAM or whatever's available. This makes for really fast allocation because no allocation of real resources took place. – user4581301 May 04 '22 at 19:38
  • I tried with calloc and it gives almost the same graph – Ivan Ivan May 04 '22 at 19:46
  • Profiling a debug build or a release build? Sometimes debug builds do helpful stuff like write an unlikely and easily identifiable pattern into memory so you can easily detect uninitialized variables. In this case the allocation would be written, and assigned real storage, with `malloc` or `calloc`. Either way, Remy's answer is still probably right. – user4581301 May 04 '22 at 20:30
  • function `clock()` usually has a very bad accuracy. Consider using `gettimeofday()` or `clock_gettime()` – tstanisl May 04 '22 at 21:35
  • @RetiredNinja Possibly not even then. Modern OS's prepare some zero-initialised pages when they have nothing better to do and then dole them out when needed, eg [like this](https://stackoverflow.com/questions/18385556/does-windows-clear-memory-pages). But you probably already knew that :) – Paul Sanders May 04 '22 at 23:23
  • @PaulSanders Yeah, that's the *might* of it. :) It used to matter, and for some platforms it may still, but for the most part it's hidden behind a lot of voodoo that isn't easily seen. The *good old days* of real physical memory addresses and consoles that could soft reboot while preserving RAM are an occasional fond memory. – Retired Ninja May 04 '22 at 23:38

1 Answers1

2

why the graph is not increasing linearly but barely increasing and there are sudden spikes?

Internally, malloc() is likely asking the OS for a larger amount of memory, and then dividing it up for subsequent allocations. The spikes are likely when malloc() has to go back to the OS to get more memory. Accordingly, free() wouldn't return memory back to the OS, it would just return it back to whatever internal pool malloc() is using to cache memory.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770