Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi, I need to know the virtual memory usage, the page faults, and other memory related information in a linux/c++ program. I need it to be posix-portable so I would ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Location
    Barcelona
    Posts
    6

    How to get the memory usage of myself process (GetProcessMemoryInfo() 4 linux)?

    Hi,
    I need to know the virtual memory usage, the page faults, and other memory related information in a linux/c++ program.
    I need it to be posix-portable so I would try to avoid reading /proc/self/stat or /proc/self/statm because it is kernel/platform dependent.
    In windows there's a function GetProcessMemoryInfo(), and GlobalMemoryStatus() which are pretty good for that.

    Is there something similar in c/c++/linux?

    Thanks
    Rafa

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Check the system sysinfo() call. It returns a sysinfo structure which is defined as follows (see /usr/include/linux/kernel.h):
    Code:
    struct sysinfo {
    	long uptime;			/* Seconds since boot */
    	unsigned long loads[3];		/* 1, 5, and 15 minute load averages */
    	unsigned long totalram;		/* Total usable main memory size */
    	unsigned long freeram;		/* Available memory size */
    	unsigned long sharedram;	/* Amount of shared memory */
    	unsigned long bufferram;	/* Memory used by buffers */
    	unsigned long totalswap;	/* Total swap space size */
    	unsigned long freeswap;		/* swap space still available */
    	unsigned short procs;		/* Number of current processes */
    	unsigned short pad;		/* explicit padding for m68k */
    	unsigned long totalhigh;	/* Total high memory size */
    	unsigned long freehigh;		/* Available high memory size */
    	unsigned int mem_unit;		/* Memory unit size in bytes */
    	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
    };
    I hope this is what you are looking for.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Location
    Barcelona
    Posts
    6
    Thanks Rubberman.

    But this is not exactly what I need. sysinfo() tells me about the system global information, but I need information about my current process. I'll be more explicit telling my needs. What I need exactly is:

    - The total amount of physical memory in the system (not virtual).
    - The total amount of virtual memory in the system.
    -The total amount of memory that my process can get.
    -The total amount of memory that my process actually has.
    -The number of page faults that my process has done. (This is what is more difficult to get)
    -The amount of locked memory for my process.

    I know that all this information can be read in /proc/self/stat , and I'm developing myself a function that reads that file and save its content to a struct. But I didn't found (in the net) reliable information of the meaning of all the fields in /proc/self/stat, and this is not as portable as I would like. All the documentation I've found is obsolete, and I've found that this file changes in different kernel versions, so it is not portable at all.

    Thanks anyway, I'll continue searching. If I find something, I'll post.

    Rafa

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Check out The Linux Documentation Project - they have a lot of Linux documentation for stuff like this. I've never needed this specific information, so I don't know what the system call is to get it, but it likely exists. I found the sysinfo() stuff by doing some quick scans thru /usr/include files and from there to the appropriate man pages.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Apr 2009
    Location
    Barcelona
    Posts
    6

    Thumbs up

    Thanks Rubberman, I'll look at it.
    By now, I need to have it fast, so I'll stay with reading /proc/self/stat . In the future I'll look for a better way.

    Thanks

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You might be interested in this article: Exploring procfs LG #115 - it describes the items in /proc and how you can get the memory usage information you want (in /proc/pid/statm).

    After some investigation, all the solutions I've found are looking in /proc/<pid>/whatever for process information such as memory usage stats. I don't know how top gets process memory information, but you could easily look at the source to see.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Just Joined!
    Join Date
    Apr 2009
    Location
    Barcelona
    Posts
    6
    Well, I've been checking the info from /proc/ filsystem and the info from sysinfo() function, and the values are not the same .

    IE: The total virtual memory is not the same in both (Must be the addition of Total Phisical Mem + Total Swap Mem). And the difference is great enough to not rely on it (about 500 Mb in a system with 10GB Virtual Memory and 4GB physical memory).

    I think (but I don't really know) that /proc/ must be more reliable.

    Can anyone confirm that?

  8. #8
    Just Joined!
    Join Date
    Apr 2009
    Location
    Barcelona
    Posts
    6
    Well, the fact is that /proc/meminfo gives exactly the same information as sysinfo(). So the problem is /proc/self/stat.

    I've also found a function getrusage() that is supposed to give me all the information that I read from /proc/self/stat, but when I use it it gives me all data set to zero. I don't know why...

  9. #9
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by faristol View Post
    Well, the fact is that /proc/meminfo gives exactly the same information as sysinfo(). So the problem is /proc/self/stat.

    I've also found a function getrusage() that is supposed to give me all the information that I read from /proc/self/stat, but when I use it it gives me all data set to zero. I don't know why...
    Me neither. I've never needed this info in a program before, so it's new ground for me as well. Anyway, from what I read, the "recommended" means of accessing this information is to open and read /proc/<pid>/statm. All (most) of the examples I have found do it that way.

    Remember, the procfs (which handles the /proc stuff) is an in-memory data set. Opening and reading one of the files should not incur the overhead of most file system calls since it doesn't need to hit disc, so if performance is your concern, then it probably won't be too onerous.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  10. #10
    Just Joined!
    Join Date
    Apr 2009
    Location
    Barcelona
    Posts
    6

    Thumbs up

    Finally I've downloaded the source code of ps (from psproc in sourceforge) and taked a look at it. So, they do look at /proc/<pid>/stat file. And if ps programmers rely on it, I will

    Conclusion: The best way is to rely on /proc filesystem and, as Rubber said, it has no much cost.

    Thanks for all.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...