Get code from here.
The header file
#include "memorymeasure.h"
provides a single function getMemory
which returns the current and peak,
real and virtual memories (in bytes, accurate to ~kilobytes) used by your C code's Linux process.
Call via
unsigned long currRealMem, peakRealMem, currVirtMem, peakVirtMem;
int success = getMemory(&currRealMem, &peakRealMem, &currVirtMem, &peakVirtMem);
where success = 0
if memory fetching was successful (if the needed linux process files exist).
Real memory (resident set size) is the amount of physical RAM your process is using, and virtual memory is the size of the memory address space your process is using. Linux chooses what in your virtual memory gets to reside in RAM. Note that in addition to your program data, these memories include the space taken up by your code itself, and any libraries your code is using (which may be shared by other running processes, skewing your usage). A good explanation can be found here.
Peak memory is the maximum amount of memory your process has used over its lifetime so far.
This function works by reading /proc/self/status
which linux populates with info about your running process.
It uses fields
Output | status Field | Description |
---|---|---|
currRealMem | VmRSS | Resident set size |
peakRealMem | VmHWM | Peak resident set size ("high water mark") |
currVirtMem | VmSize | Virtual memory size |
peakVirtMem | VmPeak | Peak virtual memory size |
taken from under /proc/[pid]/status on the proc man page.
See procps for a much more versatile tool in analysing the linux process table.
4 Comments
Tyson Jones · January 10, 2018 at 6:15 pm
Feel free to comment below (using a WordPress account) to ask questions, point out errors or suggest changes, or otherwise email me at
Simon Benjamin · April 17, 2018 at 2:38 pm
Instructions worked like a charm, thanks Tyson!
Two minor things:
1. The in-page links from the contents to the item don’t seem to work in Chrome.
2. For the examples, maybe add “gsl_matrix_get(myMatr, 1,2);” after populating the matrix, to show users that they can get the number out with …_get
Simon Benjamin · April 21, 2018 at 12:39 am
Something for the GSL Guide under “construct matrices and vectors”… It really useful to stress that the matrices and vectors are NOT zero initialised, so they are fully of random junk when instantiated (albeit the random junk is quite likely to be a zero). Programmer must take care to set every value.
I forgot this and spent ages chasing a bug.
Simon Benjamin · April 21, 2018 at 5:07 pm
Extra: I see that things like gsl_permutation_calloc (i.e. ending in calloc not alloc) will initialise to zero for you.