Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are some allocators lazy?

I wrote a C program in Linux that mallocs memory, ran it in a loop, and TOP didn't show any memory consumption.

then I've done something with that memory, and TOP did show memory consumption.

When I malloc, do I really "get memory", or is there a "lazy" memory management, that only gives me the memory if/when I use it?

(There is also an option that TOP only know about memory consumption when I use it, so I'm not sure about this..)

Thanks

like image 710
Liran Orevi Avatar asked May 14 '09 16:05

Liran Orevi


People also ask

What is lazy memory?

Lazy allocation simply means not allocating a resource until it is actually needed. This is common with singleton objects, but strictly speaking, any time a resource is allocated as late as possible, you have an example of lazy allocation.

What is lazy allocation of virtual memory?

5.2 Lazy Allocation Policy In lazy allocation, swap space is reserved dynamically as the system needs to reclaim physical memory instead of having to allocate it in advance for every page of anonymous memory (that is, memory devoted to the stack and heap of a process, and to data that is not file-backed).


2 Answers

Are you using compiler optimizations? Maybe the optimizer has removed the allocation since you're not using the allocated resources?

like image 81
Ryan Avatar answered Nov 15 '22 16:11

Ryan


On Linux, malloc requests memory with sbrk() or mmap() - either way, your address space is expanded immediately, but Linux does not assign actual pages of physical memory until the first write to the page in question. You can see the address space expansion in the VIRT column, while the actual, physical memory usage in RES.

like image 38
bdonlan Avatar answered Nov 15 '22 15:11

bdonlan