#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int i = 0;
struct rusage r_usage;
while (++i <= 10) {
void *m = malloc(20*1024*1024);
memset(m,0,20*1024*1024);
getrusage(RUSAGE_SELF,&r_usage);
printf("Memory usage = %ld\n",r_usage.ru_maxrss);
sleep (3);
}
printf("\nAllocated memory, sleeping ten seconds after which we will check again...\n\n");
sleep (10);
getrusage(RUSAGE_SELF,&r_usage);
printf("Memory usage = %ld\n",r_usage.ru_maxrss);
return 0;
}
The above code uses ru_maxrss attribute of rusage structure. It gives the value of maximum resident set size. What does it mean? Each time the program is executed it gives a different value. So please explain the output of this code?
These are screenshots of two executions of the same code which give different outputs, how can these numbers be explained or what can be interpreted from these two outputs?
The resident set size (RSS) is the amount of space of physical memory (RAM) held by a process. The value is typically specified in bytes or pages. If the full amount of space required by a process exceeds the RSS, the remaining portion is typically stored in swap.
2.1. Resident Set Size. This is a measure of how much memory a process is consuming in our physical RAM, to load all of its pages after its execution. This includes memory allocated from shared libraries, given they are still present in memory. Also, it includes all heap and stack memory.
In computing, resident set size (RSS) is the portion of memory occupied by a process that is held in main memory (RAM). The rest of the occupied memory exists in the swap space or file system, either because some parts of the occupied memory were paged out, or because some parts of the executable were never loaded.
RSS is Resident Set Size (physically resident memory - this is currently occupying space in the machine's physical memory), and VSZ is Virtual Memory Size (address space allocated - this has addresses allocated in the process's memory map, but there isn't necessarily any actual memory behind it all right now).
Resident set size (RSS) means, roughly, the total amount of physical memory assigned to a process at a given point in time. It does not count pages that have been swapped out, or that are mapped from a file but not currently loaded into physical memory.
"Maximum RSS" means the maximum of the RSS since the process's birth, i.e. the largest it has ever been. So this number tells you the largest amount of physical memory your process has ever been using at any one instant.
It can vary from one run to the next if, for instance, the OS decided to swap out different amounts of your program's memory at different times. This decision would depend in part on what the rest of the system is doing, and where else physical memory is needed.
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