0

On a Linux system, using a C++ program, I need to find a way to print out the following information:

  1. Amount of memory in use.
  2. Amount of Free memory
  3. Linux Kernel Version.

Can anyone help me on how to achieve this?

Thank you.

paulonogueira
  • 2,684
  • 2
  • 18
  • 31
Happy
  • 21
  • 2

1 Answers1

3

You could use the proc(5) file system (it is specific to Linux). You might read sequentially /proc/meminfo and /proc/version (these pseudo-files should be read sequentially, and they are quick, not involving any disk IO). You could also use uname(2) for the version of the kernel.

If you want to query about virtual memory for your own process, use /proc/self/maps

If you need information related to malloc(3), use malloc_info(3), mallinfo(3), malloc_stats(3), ...

The comment by mlwn also mentions rightly sysconf(3)

See also linuxatemyram. And read Advanced Linux Programming

It is possible to limit the used (or available) memory, see this answer. Use getrlimit(2) to query the resource limits.

PS. Your mention of amount of memory in use is very ambiguous! Notice also that syscalls(2) are not the same as system(3)

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547