LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2005, 03:43 AM   #1
gecoool
Member
 
Registered: Feb 2005
Location: Romania
Distribution: Fedora 2
Posts: 38

Rep: Reputation: 15
getting total memory used


hello everyone.

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 ;-).
 
Old 10-17-2005, 11:01 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
try rusage:
Code:
#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;
}
 
Old 10-18-2005, 01:07 AM   #3
gecoool
Member
 
Registered: Feb 2005
Location: Romania
Distribution: Fedora 2
Posts: 38

Original Poster
Rep: Reputation: 15
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.
 
Old 10-18-2005, 02:01 AM   #4
gecoool
Member
 
Registered: Feb 2005
Location: Romania
Distribution: Fedora 2
Posts: 38

Original Poster
Rep: Reputation: 15
didn't work ...

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)

Any suggestions are wellcome !

Regards,
Geo.
 
Old 10-18-2005, 11:58 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
I don't know if your kernel implements getrusage fully.

Do you know about /proc/<pid> files? You can try those.
 
Old 10-19-2005, 01:18 AM   #6
gecoool
Member
 
Registered: Feb 2005
Location: Romania
Distribution: Fedora 2
Posts: 38

Original Poster
Rep: Reputation: 15
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 ?
 
Old 10-19-2005, 11:21 PM   #7
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
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.
 
Old 10-20-2005, 01:59 AM   #8
gecoool
Member
 
Registered: Feb 2005
Location: Romania
Distribution: Fedora 2
Posts: 38

Original Poster
Rep: Reputation: 15
Hello primo ;-)

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 ?

Thanks a lot, it really helped ;-)
 
Old 10-20-2005, 02:23 AM   #9
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ipcs -lm shows : max total shared memory (kbytes) = 0 vicv Linux - Software 1 09-01-2005 10:58 AM
Boot Hangs with Redhat, GRUB, Total HugeTLB memory allocated, Neochubbz Linux - General 5 08-19-2004 01:27 PM
shared Memory versus the total amount of RAM on the server ashley75 Linux - General 1 08-25-2003 11:21 AM
Help!?! RH 8 Memory Mapping -High Memory-Virtural Memory issues.. Merlin53 Linux - Hardware 2 06-18-2003 04:48 PM
Limiting the total amount of memory usage by apache. SplashHost.com Linux - General 8 08-30-2002 09:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration