Need to write a c code to find the total memory consumed by a process

Need to write a c code to find the total memory consumed by a process

Post by Manish Baroni » Tue, 22 Jul 2003 15:38:33



Hi All,
    I need to write a module in C which calculates the total memory
consumed by a process (text, data, shared). Can some one tell me if
there are APIs available in C for this.
Thanks.
Manish
 
 
 

Need to write a c code to find the total memory consumed by a process

Post by Thomas Glanzman » Tue, 22 Jul 2003 15:42:17


Hi,

Quote:> I need to write a module in C which calculates the total memory
> consumed by a process (text, data, shared). Can some one tell me if
> there are APIs available in C for this.

look at the source code of Proc::ProcessTable [1].

Greetings,
        Thomas

[1] Proc::ProcessTable
    http://search.cpan.org/CPAN/authors/id/D/DU/DURIST/Proc-ProcessTable-...

 
 
 

Need to write a c code to find the total memory consumed by a process

Post by Shaun Clowe » Tue, 22 Jul 2003 15:47:42



Hi Manish,

Quote:>     I need to write a module in C which calculates the total memory
> consumed by a process (text, data, shared). Can some one tell me if
> there are APIs available in C for this.

There aren't any APIs that I'm aware of, but you should check out the
proc(4) interface. In particular it provides a /proc/<pid>/map file which
contains the data you're looking for. The pmap(1) command display's this
information.

Cheers,
Shaun

 
 
 

Need to write a c code to find the total memory consumed by a process

Post by Manish Baroni » Tue, 22 Jul 2003 17:05:40


Hello Thomas,
      Thanks for the info. I downloaded the code, but thats in Perl. But I need it in
C.
Regards,
Manish

> Hi,

> > I need to write a module in C which calculates the total memory
> > consumed by a process (text, data, shared). Can some one tell me if
> > there are APIs available in C for this.

> look at the source code of Proc::ProcessTable [1].

> Greetings,
>         Thomas

> [1] Proc::ProcessTable
>     http://search.cpan.org/CPAN/authors/id/D/DU/DURIST/Proc-ProcessTable-...

 
 
 

Need to write a c code to find the total memory consumed by a process

Post by Manish Baroni » Tue, 22 Jul 2003 17:13:31


Thanks Shaun,
       I have seen the output of  pmap, and thats exactly what I want. But I
want to make a C code for that
Just wonder, how "top" utility calculates it.
Thanks,
Manish



> Hi Manish,

> >     I need to write a module in C which calculates the total memory
> > consumed by a process (text, data, shared). Can some one tell me if
> > there are APIs available in C for this.

> There aren't any APIs that I'm aware of, but you should check out the
> proc(4) interface. In particular it provides a /proc/<pid>/map file which
> contains the data you're looking for. The pmap(1) command display's this
> information.

> Cheers,
> Shaun

 
 
 

Need to write a c code to find the total memory consumed by a process

Post by Michael Lehman » Tue, 22 Jul 2003 17:36:04


Hi Manish,

how about downloading the sources for top ?

http://www.spang.uk.eu.org/top/

Michael


> Thanks Shaun,
>        I have seen the output of  pmap, and thats exactly what I want. But
I
> want to make a C code for that
> Just wonder, how "top" utility calculates it.
> Thanks,
> Manish




> > Hi Manish,

> > >     I need to write a module in C which calculates the total memory
> > > consumed by a process (text, data, shared). Can some one tell me if
> > > there are APIs available in C for this.

> > There aren't any APIs that I'm aware of, but you should check out the
> > proc(4) interface. In particular it provides a /proc/<pid>/map file
which
> > contains the data you're looking for. The pmap(1) command display's this
> > information.

> > Cheers,
> > Shaun

 
 
 

Need to write a c code to find the total memory consumed by a process

Post by Thomas Glanzman » Tue, 22 Jul 2003 20:03:09


Quote:> Thanks for the info. I downloaded the code, but thats in Perl. But I
> need it in C.

crown wheel: Have a closer look! The whole code is in C. Idiot!
 
 
 

Need to write a c code to find the total memory consumed by a process

Post by Shaun Clowe » Wed, 23 Jul 2003 08:22:28



Quote:> Thanks Shaun,
>        I have seen the output of  pmap, and thats exactly what I want. But
I
> want to make a C code for that
> Just wonder, how "top" utility calculates it.

"top" uses the /proc filesystem rather like everything else that needs
process information. In particular it reads the /proc/<pid>/psinfo file to
get the following fields from the psinfo_t data structure:

          size_t pr_size;           /* size of process image in Kbytes */
          size_t pr_rssize;         /* resident set size in Kbytes */

Read the proc(4) man page and all will become clear.

pmap on the other hand reads the /proc/<pid>/map file like I told you. It's
simple to read like this:

#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <procfs.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
   int iRc;
   int iMapFd;
   char sFileName[1024];
   prmap_t tMap;
   pid_t tPid = atoi(argv[1]);

   sprintf(sFileName, "/proc/%d/map", tPid);
   iMapFd = open(sFileName, O_RDONLY);
   printf("Map %d\n", iMapFd);

   /* Read the map */
   while (read(iMapFd, &tMap, sizeof(tMap)) == sizeof(tMap))
      if (tMap.pr_size)
         printf("0x%08x %d %d %s\n", tMap.pr_vaddr, tMap.pr_size, (int)
tMap.pr_offset, tMap.pr_mapname);

   return(0);

Quote:}

Hopefully you can see how to get what you want from here.

Cheers,
Shaun

 
 
 

Need to write a c code to find the total memory consumed by a process

Post by Manish Baroni » Wed, 23 Jul 2003 22:29:08


Hello Shaun,
    It works for me.
Thanks,
Manish



> > Thanks Shaun,
> >        I have seen the output of  pmap, and thats exactly what I want. But
> I
> > want to make a C code for that
> > Just wonder, how "top" utility calculates it.

> "top" uses the /proc filesystem rather like everything else that needs
> process information. In particular it reads the /proc/<pid>/psinfo file to
> get the following fields from the psinfo_t data structure:

>           size_t pr_size;           /* size of process image in Kbytes */
>           size_t pr_rssize;         /* resident set size in Kbytes */

> Read the proc(4) man page and all will become clear.

> pmap on the other hand reads the /proc/<pid>/map file like I told you. It's
> simple to read like this:

> #include <errno.h>
> #include <string.h>
> #include <unistd.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <procfs.h>
> #include <fcntl.h>

> int main(int argc, char *argv[])
> {
>    int iRc;
>    int iMapFd;
>    char sFileName[1024];
>    prmap_t tMap;
>    pid_t tPid = atoi(argv[1]);

>    sprintf(sFileName, "/proc/%d/map", tPid);
>    iMapFd = open(sFileName, O_RDONLY);
>    printf("Map %d\n", iMapFd);

>    /* Read the map */
>    while (read(iMapFd, &tMap, sizeof(tMap)) == sizeof(tMap))
>       if (tMap.pr_size)
>          printf("0x%08x %d %d %s\n", tMap.pr_vaddr, tMap.pr_size, (int)
> tMap.pr_offset, tMap.pr_mapname);

>    return(0);
> }

> Hopefully you can see how to get what you want from here.

> Cheers,
> Shaun

 
 
 

1. Memory use: which process is consuming it all?

I'm running Slackware 2.0.0, and am finding that ~9 megs of memory are
already used when I boot up.  When I run X and Netscape, that has gone up
to 30 megs.  (These numbers come from 'free'.)  Is this normal?  (It
sounds way too high to me!)  Also, if it _is_ true, why doesn't the memory
usage show upusing ps -amp (all processes + memory info)

eg: right now free gives
             total       used       free     shared    buffers     cached
Mem:         30892      30380        512      21988       1428      12668
-/+ buffers:            16284      14608
Swap:        34268          0      34268

and ps -amp only lists X and Netscape as using over 1 meg (1.4 and 2.6
respectively).  The cache setting in Netscae is set to 3 meg, so it
shouldn't be the problem.  Any ideas/hints would be most appreciated!

Thanks in advance,

Alan Ableson

2. Konqureor plugin

3. Total available free memory for new processes ?

4. libSM.so libICE.so

5. The total size of the process in virtual memory

6. USB Printer

7. How to find out the memory size used by a process or check its memory leaking?

8. bash v1.13cwru breaks elm

9. Need coding advice for "total history" recording

10. total, total newbie needs help with general unix

11. finding total memory allocated

12. question about process memory usage and process memory limits

13. Free Virutal Memory is 1% even though Total Free Virtual Memory is above 1GB