Have you tried cat /proc/<your pid>/status
Andrei
Does this answer your question?
http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#memsize
--
Kasper Dupont
I've wrote the following simple experiement program:
#include <stdio.h>
#include <stdlib.h>
int main(){
int *mem, count = 0;
while(1){
mem = realloc(NULL, count * sizeof(int));
printf("count = %d\n", count);
getchar();
count += 10000;
}
free(mem);
return 0;
}
I used "top" to find out its pid, then look at its status at each
iteration, the following is what the status looks like after 6
iterations (in KB):
| Iteration
| 0 1 2 3 4 5
-------+------------------------------
VmSize | 1284 1320 1396 1512 1672 1868
VmLck | 0 0 0 0 0 0
VmRSS | 316 324 328 332 336 340
VmData | 24 60 136 252 412 608
VmStk | 12 12 12 12 12 12
VmExe | 4 4 4 4 4 4
VmLib | 1220 1220 1220 1220 1220 1220
Since integer takes up 4 bytes, the memory usage should increase 10000
x 4 bytes (approximately 40 KB) after each "realloc" (logically
speaking). This pretty much rules out VmLck, VmStk, VmExe, and VmLib
because they're constant.
VmRSS was steadily incrementing 4 KB at a time, but it can't be that
because I'm looking for 40 KB increment. The process of elimination
only left VmSize and VmData, it appears that VmSize = VmData + 1260
KB, so VmData looks that the right field that I'm after. But how come
the memory increase grew out of proportion after the first iteration?
Does the VmData field correctly represents my program's memory usage
(I'm more interested in data usage)? Say if I have the following
structure:
typedef struct mystruct{
char x; // 1 byte
short int y; // 2 bytes
int z; // 4 bytes
struct MYSTRUCT *left; // 4 bytes
struct MYSTRUCT *right; // 4 bytes
}MYSTRUCT;
MYSTRUCT ms = malloc(10000 * sizeof(MYSTRUCT));
Is it fair to say my program (the data portion) uses 10000 x (1 + 2 +
4 + 4 + 4) = 150000 bytes or roughly 150 KB of memory?
Thanks in advance for any thoughts.
p190...@yahoo.com wrote in message news:<1097134168.2...@f14g2000cwb.googlegroups.com>...
VmSize is the total size of your virtual memory
VmRSS is the size of the actual physical memory that your program uses.
(some memory can be swapped out or not initialized).
VmData is the size of your "data segment".
VmStk is the size of your "stack segment".
>
> Since integer takes up 4 bytes, the memory usage should increase 10000
> x 4 bytes (approximately 40 KB) after each "realloc" (logically
> speaking). This pretty much rules out VmLck, VmStk, VmExe, and VmLib
> because they're constant.
This mathematic does not work. malloc and friends allocate memory from
the "heap". This memory is usually already given to the process, so you
don't see the result of malloc in the /proc/<pid>/status. Internally,
when all "heap" memory of the process is exausted, malloc requests from
OS to give more memory to the process. Then some big chunk is added to
the VmSize (and VmData). From that point on malloc will allocate memory
from this new chunk (of course this is true only for the case when no
memory is freed). As result the VmRSS shall also grow. (Again VmRSS
won't necessarily grow by the amount requested from malloc)
So if you expected, that using OS calls, you can find out how your program
uses memory, then probably you expectations have failed. OS does not
care/know how your application uses "data" memory. OS is just
responsible for "assigning" memory to your application, and only your
application knows what is placed in that data area. Of course with the
help of debugger you can check it :)
Summary. OS can tell you memory usage of your program. But this memory
usage does not follow specific malloc/free calls.
Andrei
mem = realloc(NULL, count * sizeof(int));
You are realloc-ing NULL; in other words, you leak "mem" each iteration
and allocate a new block rather than resizing the original.
--
Andrew