简体   繁体   中英

Linux: /proc/self/statm is it trustable?

My main task is to find out how much memory a process is using to do different things. I am reading the RSS from statm file before and after doing something, then I subtract this two values to know how much memory the process is using to do this something.

For example, in this picture you will see the memory I measured to multiply sparse matrices of different sizes and densities. Notice how odd it is that matrices of size 100x100,200x200 and 300x300 take nothing into consideration on the RSS increase. A bunch of other stuff I am doing I am also getting odd zeros. Am I missing something here? Am I measuring the memory the wrong way? Please fell free to point out any better way you know to measure memory usage by piece of code.

I tried using rgetusage that brings the peak usage by the process and it seems worst.

EDIT: I am coding on C++. I am allocating the matrices outside of main with a function using malloc:

    int **createMatrix(int N, int M)
{
    int i, **table;
    table = (int**)malloc(N*sizeof(int *));
    for(i = 0 ; i < N ; i++)
        table[i] = (int*)malloc( M*sizeof(int) );
    return table;
}

massif is fairly good at tracking down memory usage in the general case. I would recommend it in conjunction with massif-visualizer

Regarding the 0's, keep in mind that this is OS memory. Most likely your code is not allocating directly from the OS, but from an allocator in a standard library (such as the default malloc allocator in the standard C library). These allocate large blocks from the OS and then split them up to fulfill allocation requests. If there was enough free space in an existing block to satisfy the allocation request, no more blocks are requested. Things get more complicated if multiple threads are involved.

More fine-grained tracking than what is in proc would require you to tell us the programming language and allocation mechanisms used. Most allocators provide these stats somewhere. For example, GNU libc has mallinfo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM