Reputation: 9
A little bit of the context: I try to implement a C++ application that uses mmap
to map some arbitrary large files for reading and writing, which can scale from a few MB to several GB. Due to this, it is important to profile the memory usage (peak RSS, I want to see how uch physical memory it consumes) of the program in order to see its performance.
I use Valgrind's massif tool with the option pages-as-heap=yes
and massif visualizer
. I expect that this will show me the peak RSS. I run the program with mmap
reserving exactly 1GB. The massif-visualizer showed me exactly what was expected (1GB peak) (see image).
I also used \time -v
command, and this showed that the max RSS was very small in size (5000 kbytes, more or less). Here's one example output:
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 112%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 4772
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 1928
Voluntary context switches: 32
Involuntary context switches: 1
Swaps: 0
File system inputs: 0
File system outputs: 88
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
I also modified the program so time
would run much longer, in case the delta for memory snapshots is not sufficient, but I got the same results.
Why the memory peaks are different? From what I read on several posts, people expect massif with pages-as-heap=yes
option, will show you the maximum RSS which will be its peak. However, there is a difference on what \time
command outputs showing a much smaller peak. I suspect that the snapshots of massif are about the virtual memory, but correct me if I am wrong. Furthermore, I would appreciate it if anyone very familiar with massif would describe how it works and if there is a way to get max RSS. Thanks in advance!
EDIT: The answer here seems to partly answer my question: Does mmap or malloc allocate RAM?
As far as I understand, when you map files to memory they occupy the virtual space and not the physical memory space, in most OS systems. However, the mapped pages start to occupy physical memory space as soon as they become dirty, ie something is is written on them.
So, back to my original question, could this imply that valgrind massif with flag --pages-as-heap=yes
tracks the virtual memory space, whilst the \time -v
shows the physical memory less due to the fact that the pages were not modified?
Upvotes: 0
Views: 893