Link to home
Start Free TrialLog in
Avatar of DebMohanty
DebMohanty

asked on

Getting information about memory

Hi experts,

Can someone help me write a function which will print how much memory has been allocated to the program ?

e.g.

int main() {
// lots of new here
print_memory();
// lots of new again
print_memory();
// deletes are called here
print_memory();
}

print_memory in all cases should print the memory taken by this program at that particular time.
Avatar of jkr
jkr
Flag of Germany image

Hmm, what OS are you targetting?
Avatar of Intern
Intern

listening
The solution is going to be platform dependent:

If you're running on linux, you can get all kinds of good information from the sysinfo structure.

#include "linux/kernel.h"

main ()
{
 struct sysinfo Sysinfo;

 sysinfo (&Sysinfo);

/* Print any/all of the sysinfo items that you want */
}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DebMohanty

ASKER

I should have written in the first place itself, I need the solution for Solaris and AIX. It is okay to have platform dependent solutions.

Just want to reiterate that I need the information about the self program. So how does a running program know its pid ?
Are you Debdulal Mohanty from IBM, Bangalore??
No.
Just a small advise:
I had a similar task about a year ago and I downloaded the linux source code for the top-utility and learned how to do it.
Unfortunately I do not have the result code anymore, but you could do the same "learning process".

Or just compile and use this open source utility "top" if it is possible in your task.
AIX has a similar structure.

cd /usr/include/sys
grep sysinfo *.h

Will show you the header files that use sysinfo or the 64 bit version.  Basically, check out encap.h and space.h.


Kdo
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to Kdo

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer