What Happens When You malloc 2 GB but Don’t Use It?
-
malloc
reserves virtual memory, not physical memory:- When you call
malloc(2GB)
, the operating system reserves 2 GB of address space for your process in the virtual memory. - No physical RAM is allocated yet because you haven’t accessed (touched) the memory.
- When you call
-
RES
andVIRT
behavior:-
VIRT
will increase by 2 GB because the reserved memory adds to your process's virtual address space. -
RES
will not increase at this point because physical memory (RAM) has not been allocated.
-
-
When does
RES
increase?- Physical memory is only allocated when you touch (read/write) the memory.
- For example, if you write to a page of the allocated memory, the kernel will allocate a physical RAM page to your process. This will reflect in the
RES
size.
- For example, if you write to a page of the allocated memory, the kernel will allocate a physical RAM page to your process. This will reflect in the
- Physical memory is only allocated when you touch (read/write) the memory.
Question:
If I malloc 2 GB
of memory but don’t use it (i.e., the pages
are allocated virtually but not yet touched), will it count toward the RES
(resident memory)? And since the memory hasn't been touched, can other processes still use that space?
1. If I malloc
2 GB and do not use it, will it be counted in RES
?
No, it will not be counted in RES
.
Here’s why:
- When you call
malloc(2GB)
, the operating system reserves virtual address space for the requested memory. - However, no physical memory (RAM) is allocated until you actually access or "touch" those pages.
- This is due to the lazy allocation strategy used by modern operating systems. Pages are only backed by physical memory (loaded into RAM) when they are accessed for the first time.
In top
command:
-
VIRT
will increase by 2 GB because the virtual memory address space has been reserved. -
RES
will remain unchanged because no physical memory is used yet.
2. What happens when you “touch” the memory?
The first time you write to a page in the allocated memory:
The operating system generates a page fault.
It assigns a physical page (RAM) to the virtual address space.
That page now counts towards the RES value.
So, RES only grows as you actually use the memory.
3. Can other processes use the unallocated memory (from malloc
)?
Yes, absolutely.
Here’s why:
- When you call
malloc
, you are only reserving virtual address space in your process. The physical memory (RAM) is not yet allocated. - Since the physical RAM is not committed to your process, it remains free for other processes to use.
- Until you “touch” the memory (write to it), the operating system doesn’t allocate RAM to it.
Analogy to Simplify
Think of the OS as a hotel manager and memory as rooms:
- Virtual Address Space: You’ve “booked” 2 GB of rooms (via malloc), but the hotel manager only writes your name in the reservation book.
- Physical Memory: Rooms in the hotel (RAM). The manager doesn’t hand over any rooms to you yet.
- Touching Memory: When you enter the rooms (write to the memory), the manager actually allocates rooms (physical memory) for you.
- Until you use the rooms, they are still available for other guests (processes).
- Your name in the reservation book (
VIRT
) just says you’ve reserved them if needed.
Quick Example with Code
Here’s a C example to test this:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
printf("PID: %d\n", getpid());
printf("Allocating 2 GB using malloc...\n");
char *ptr = malloc(2L * 1024 * 1024 * 1024); // Allocate 2 GB
if (!ptr) {
perror("malloc failed");
return 1;
}
printf("Press Enter to touch memory...\n");
getchar(); // Pause to check top command before touching
for (long i = 0; i < 2L * 1024 * 1024 * 1024; i += 4096) {
ptr[i] = 0; // Touch memory 4KB at a time
}
printf("Memory touched. Press Enter to exit...\n");
getchar(); // Pause to check top command after touching
free(ptr);
return 0;
}
Steps to Test:
Compile and run the program: gcc test.c -o test && ./test
Note the PID and check the top command output:
-
Before touching memory:
-
VIRT
increases by 2 GB. -
RES
remains small.
-
-
After touching memory:
- RES increases to reflect the actual physical memory used.
Conclusion
-
malloc
without usage:- Increases
VIRT
, but notRES
- Physical RAM is not allocated.
- Increases
-
Other processes:
- The unallocated memory is still free for other processes to use.
- Physical RAM is only committed to your process when you "touch" the memory.
-
Key Point:
-
malloced
but unused memory increasesVIRT
but notRES
. - Virtual memory reservation (
VIRT
) does not mean physical memory usage (RES
). - Physical memory is allocated lazily – only when you access the memory.
-
Let me know if you'd like further clarification! 🚀
Top comments (0)