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?
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 .
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.
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.
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.
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.
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.
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.
“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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With