Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does malloc reserve more space while allocating memory?

I am observing the following behavior in my test program:

I am doing malloc() for 1 MB and then free() it after sleep(10). I am doing this five times. I am observing memory consumption in top while the program is running.

Once free()-d, I am expecting the program's virtual memory (VIRT) consumption to be down by 1 MB. But actually it isn't. It stays stable. What is the explanation for this behavior? Does malloc() do some reserve while allocating memory?

like image 269
user1228352 Avatar asked Mar 22 '19 07:03

user1228352


People also ask

Does malloc allocate extra memory?

The malloc function allocates a memory block of at least size bytes. The block may be larger than size bytes because of the space that's required for alignment and maintenance information. malloc sets errno to ENOMEM if a memory allocation fails or if the amount of memory requested exceeds _HEAP_MAXREQ .

How much space does malloc allocate?

Malloc(12) and malloc(16) allocate 16 bytes for the user, plus an extra 8 bytes for bookkeeping for a total of 24 bytes. Malloc(100) allocates 104 bytes for the user, plus an extra 8 bytes for bookkeeping.

How does malloc Reserve memory?

C malloc() The malloc() function reserves a block of memory of the specified number of bytes. And, it returns a pointer of void which can be casted into pointers of any form.

Where is the memory space reserved when calling malloc?

You can call the malloc function at any time, and it will request a block of memory from the heap. The operating system will reserve a block of memory for your program, and you can use it in any way you like.

How much memory does malloc take up?

The first call to malloc will reserve a minimum of 1 page of memory (usually 4KB) or some multiple of a page from the OS . The OS kernel generally deals in pages only.

What does allocate mean in malloc?

The word “allocate” has a couple of meanings.malloc usually runs on operating systems that have ways of acquiring (allocating) memory from the operating system. This can be done on Linux for example using the “sbrk” system call but more recently there are other ways that also allow recycling memory back to the operating system.

Why do we use malloc in C?

In compiled programming languages ​​like C, it is often useful, or even necessary, to allocate memory dynamically on the heap, in order to accommodate variables of larger or uncertain size. The malloc function allows us to ask the operating system to allocate an area of ​​memory to be used in our program.

How to allocate large blocks of memory in C?

“malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It initializes each block with default garbage value. Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory.


1 Answers

Once free()-d, I am expecting program's virtual memory (VIRT) consumption to be down by 1MB.

Well, this is not guaranteed by the C standard. It only says, once you free() the memory, you should not be accessing that any more.

Whether the memory block is actually returned to the available memory pool or kept aside for future allocations is decided by the memory manager.

like image 109
Sourav Ghosh Avatar answered Sep 29 '22 23:09

Sourav Ghosh