Why does the malloc function in C-language allocate more memory than the machine configuration?

Viewed 65

I'm allocating memory using malloc function in C, but I'm having a weird problem and I'm confused about it.

Below is the program code:

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

#define _1M (1024 * 1024)

// TODO stackOverFlow question
int main(int c, char *v[]) {

    size_t max_allocatable_mem;
    long i = 1;
    char *addr = NULL;

    while (1) {

        max_allocatable_mem = _1M * i;
        addr = malloc(max_allocatable_mem);

        if (addr) {
            printf("allocatable % 16ld M\n", i++);
            free(addr);
        } else {
            printf("can not be allocated % 16ld M\n", i--);
            break;
        }

    }

    printf("\nmax_allocatable_mem = %ld M\n\n", i);

    size_t small_mem_block = max_allocatable_mem / 10;

    for (i=0; i<20; i++) {
        addr = malloc(small_mem_block);
        if (addr) {
            printf("starting address < %p >  capacity < %lu bytes >\n", addr, small_mem_block);
        } else {
            break;
        }
    }

    return 0;
}

The program initially uses the malloc function to allocate the maximum memory, and then records the maximum memory that can be allocated. Then, the allocation of small memory is carried out. The small memory allocated each time is one-tenth of the maximum allocatable memory. It is allocated 20 times in a row, and each time malloc can return a specific memory address. In this way, the small memory allocated later adds up to twice the maximum memory the program can allocate in the first place.

Here is the output of the result of executing this code on my computer:

allocatable             1332 M
allocatable             1333 M
allocatable             1334 M
allocatable             1335 M
allocatable             1336 M
can not be allocated             1337 M

max_allocatable_mem = 1336 M

starting address < 0x7f8787a4c010 >  capacity < 140194611 bytes >
starting address < 0x7f877f498010 >  capacity < 140194611 bytes >
starting address < 0x7f8776ee4010 >  capacity < 140194611 bytes >
starting address < 0x7f876e930010 >  capacity < 140194611 bytes >
starting address < 0x7f876637c010 >  capacity < 140194611 bytes >
starting address < 0x7f875ddc8010 >  capacity < 140194611 bytes >
starting address < 0x7f8755814010 >  capacity < 140194611 bytes >
starting address < 0x7f874d260010 >  capacity < 140194611 bytes >
starting address < 0x7f8744cac010 >  capacity < 140194611 bytes >
starting address < 0x7f873c6f8010 >  capacity < 140194611 bytes >
starting address < 0x7f8734144010 >  capacity < 140194611 bytes >
starting address < 0x7f872bb90010 >  capacity < 140194611 bytes >
starting address < 0x7f87235dc010 >  capacity < 140194611 bytes >
starting address < 0x7f871b028010 >  capacity < 140194611 bytes >
starting address < 0x7f8712a74010 >  capacity < 140194611 bytes >
starting address < 0x7f870a4c0010 >  capacity < 140194611 bytes >
starting address < 0x7f8701f0c010 >  capacity < 140194611 bytes >
starting address < 0x7f86f9958010 >  capacity < 140194611 bytes >
starting address < 0x7f86f13a4010 >  capacity < 140194611 bytes >
starting address < 0x7f86e8df0010 >  capacity < 140194611 bytes >

Why can the capacity of successively allocating multiple small memory add up to more than one allocating the largest memory? Could it be the influence of Swap? But Swap is turned off on my computer.

[root@ps-5 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1881820      434676      462456         632      984688     1288636
Swap:             0           0           0

I hope to get help.

Thank you all.

0 Answers
Related