Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can static memory be lazily allocated?

Tags:

c

linux

memory

Having a static array in a C program:

#define MAXN (1<<13)
void f() {
    static int X[MAXN];
    //...
}

Can the Linux kernel choose to not map the addresses to physical memory until the each page is actually used? How can X be full of 0s then, is the memory zeroed when each page is accessed? How does that not impact the performance of the program?

like image 571
EmmanuelMess Avatar asked Jun 18 '18 02:06

EmmanuelMess


People also ask

What are the limitations of static memory allocation?

Disadvantages of Static Memory allocationThis allocation method leads to memory wastage. Memory cannot be changed while executing a program. Exact memory requirements must be known. If memory is not required, it cannot be freed.

Why is heap memory not static?

Static allocation allocates memory on the basis of the size of data objects. Heap allocation makes use of heap for managing the allocation of memory at run time. 2. In static allocation, there is no possibility of the creation of dynamic data structures and objects.

Is static memory on the stack?

Stack and a Heap ? Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM . Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled.

Do static variables go on the heap?

After the java 8 version, static variables are stored in the heap memory.


2 Answers

Can the Linux kernel choose to not map the addresses to physical memory until the each page is actually used?

Yes, it does this for all memory (except special memory used by drivers and the kernel itself).

How can X be full of 0s then, is the memory zeroed when each page is accessed?

You're supposed to ignore this detail. As long as the memory is full of zeroes when you access it, we say it's full of zeroes.

How does that not impact the performance of the program?

It does.

like image 83
user253751 Avatar answered Sep 22 '22 18:09

user253751


Can the Linux kernel choose to not map the addresses to physical memory until the each page is actually used?

Yes, with userspace memory it is always done.

How can X be full of 0s then, is the memory zeroed when each page is accessed?

The kernel maintains a page full of 0s, when the user asks for a new page of the static array (static thus full of 0s before first use), the kernel provides the zeroed page, without permissions for the program to write. Writing to the array causes the copy-on-write mechanism to trigger: a page fault occurs, the kernel then allocates a writable page, maps it and resumes the program from the last instruction (the one that couldn't complete because of the page fault). Note that prezeroing optimizations change the implementation details here, but the theory's the same.

How does that not impact the performance of the program?

The program doesn't have to zero a (potentially) lot of pages on start, and the kernel doesn't actually have to have the memory (one can ask for more memory than the system's got, as long as you don't use it). Page faults will be generated during the execution of the program, but they can be minimized, see mmap() and madvise() with MADV_SEQUENTIAL. Remember that the Translation Lookaside Buffer is not infinite, there are so many entries it can maintain.

Sources: A linux memory FAQ, Introduction to Memory Management in Linux by Alan Ott

like image 41
EmmanuelMess Avatar answered Sep 25 '22 18:09

EmmanuelMess