ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to determine the total amount of memory used at a given moment, by an application written in c++. Actaully I have a bad memory leak, and the tools i tried (i.e. valgrind) shows me a lot of "possibly lost" memory, but definitely lost is very less. So I decided to separately run each relevant section of my code and try to print the total memory used at some meaningfull moments (i.e. before and after some calls ;-)). If anyone could suggest me how to determine programatically the memory used by an application (c++), please do so. Answers like top -p my_app_pid, isn't quite the expected one ;-).
#include <sys/resource.h>
#define ck(x) if( (x)==(-1)){ perror(""); exit(EXIT_FAILURE);}
/* this is a rsuage struct */
/*
struct rusage {
struct timeval ru_utime; /o user time used o/
struct timeval ru_stime; /o system time used o/
/o actual values kept in proc struct o/
long ru_maxrss;
#define ru_first ru_ixrss
long ru_ixrss; /o integral shared memory size o/
long ru_idrss; /o integral unshared data " o/
long ru_isrss; /o integral unshared stack " o/
long ru_minflt; /o page reclaims o/
long ru_majflt; /o page faults o/
long ru_nswap; /o swaps o/
long ru_inblock; /o block input operations o/
long ru_oublock; /o block output operations o/
long ru_ioch; /o # of characters read/written o/
long ru_msgsnd; /o messages sent o/
long ru_msgrcv; /o messages received o/
long ru_nsignals; /o signals received o/
long ru_nvcsw; /o voluntary context switches o/
long ru_nivcsw; /o involuntary " o/
#define ru_last ru_nivcsw
}; */
void foo(void)
{
/* do stuff */
}
int main()
{
struct rusage start={0,0};
struct rusage end={0,0};
struct rusage *pend=&end;
struct rusage *pstart=&start;
ck(getrusage(RUSAGE_SELF,pstart)); /* get start point */
foo(); /* run function(s) */
ck(getrusage(RUSAGE_SELF,pend)); /* get end point */
/* print the diff of pend and pstart values for memory items you want here */
printf("difference: %d\n", pend->ru_ixrss - pstart->ru_ixrss);
return 0;
}
Thank you very much, jim mcnamara ;-). i tried in the mean time vtimes (obsolete, it seems), but i never figured out that i had to compute a difference ;-) (for getrusage neighter vtimes) . I thought it would return the "memory status"at a given time.
well, sadly, it didn't work ... and i don't have a single clue why ... I make the call of my function from within a block like:
Code:
ck(getrusage(RUSAGE_SELF,pstart)); /* get start point */
my_func();
ck(getrusage(RUSAGE_SELF,pend)); /* get end point */
std::cout<<pend->ru_ixrss - pstart->ru_ixrss<<" pend: "<<pend->ru_ixrss<<std::endl;
The difference pend->ru_ixrss - pstart->ru_ixrss is allways 0. I printed out pend->ru_ixrss, which is also 0.
What I did wrong ? (I included the header, etc, the program did compile and run)
thx again for your time. i don't really know what's up with /proc/pid files
can you please point me to an url, where i could find some expalnations for it ?
getrusage() isn't completely implemented in linux.
The best way is with /proc/<pid>/stat (or "/proc/self/stat"). This is what ps(1) does. Check the proc(5) manpage. The vsize value is probably what you need to check for.
Actually your advice was great ;-) ... i come over my answer and also other usefull things. Now, I suppose all it left for me to do, is to parse that file /proc/<pid>/stat, on a timely basis (i.e. every seccond or so), right ?
Well placed calls can help to track down the problem. Parsing it every second would be overkill. Many things can be done, including a child process tracking the parent.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.