Why is malloc not filling up memory?

I have the following code:

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

int main() {
    int data = 0;
    char *byte = (char *)malloc(sizeof(char)*1000000000);
byte[999999999] = 'a';
printf("%c",byte[999999999]);
    scanf("%d",&data);
    return 0;
}

      

Looking at the memory before running the program and before scanning, I expect the memory to increase by 1 GB. Why isn't this happening?

Edit: I added

byte[999999999] = 'a';
printf("%c",byte[999999999]);

      

The program outputs a

.

0


source to share


3 answers


By default Linux allocates physical memory lazily the first time it is accessed. Your call malloc

will allocate a large block of virtual memory, but there are no physical memory pages on it yet. The first access to an unprinted page will cause an error, which the kernel will handle by allocating and mapping one or more pages of physical memory.

To allocate all of the physical memory, you need to get at least one byte per page; or you can work around malloc

and go straight to the operating system with something like



mmap(0, 1000000000, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);

      

Note the use MAP_POPULATE

for populating page tables, i.e. to immediately allocate physical memory. According to the man page, this only works on fairly recent versions of Linux.

+8


source


Most (all?) Linux systems will recompile memory. This means malloc never fails, even if you reserve ridiculous amounts of memory, if you use a certain commit strategy. In some cases it can crash if you reserve too much memory, for example more than virtual memory.

You have no real memory addresses, just a pointer that is supposed to point to enough memory to use. The system allocates memory for you if you try to use it, so you need to write to it to signal the system you really want to.



This system is used because many programs do not need memory, which they reserve or need it at other times.

After editing, you only get access to one page, so you only get one page. You need to access every page to get all the memory.

+5


source


malloc

didn't give you everything 1000000000

when you ask. It's just the 1st

page you get until you start getting it (read / write), the rest of it will be assigned to you.

Linux, malloc

requests will automatically expand the virtual address space, but will not assign pages to physical memory until you access it.

0


source







All Articles