?
Solved

Getting information about memory

Posted on 2003-03-25
10
Medium Priority
?
204 Views
Last Modified: 2010-07-27
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.
0
Comment
Question by:DebMohanty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8205223
Hmm, what OS are you targetting?
0
 
LVL 1

Expert Comment

by:Intern
ID: 8205274
listening
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8205845
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 */
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 8205885
As the Linux solution is already there: On Win32, use 'GetProcessMemoryInfo()' (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/getprocessmemoryinfo.asp) - see also http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/collecting_memory_usage_information_for_a_process.asp ("Collecting Memory Usage Information For a Process")

#include <windows.h>
#include <stdio.h>
#include "psapi.h"

void PrintMemoryInfo( DWORD processID )
{
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );

    // Print information about the memory usage of the process.

    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                    PROCESS_VM_READ,
                                    FALSE, processID );
    if (NULL == hProcess)
        return;

    if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
    {
        printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
        printf( "\tPeakWorkingSetSize: 0x%08X\n",
                  pmc.PeakWorkingSetSize );
        printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
        printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n",
                  pmc.QuotaPeakPagedPoolUsage );
        printf( "\tQuotaPagedPoolUsage: 0x%08X\n",
                  pmc.QuotaPagedPoolUsage );
        printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n",
                  pmc.QuotaPeakNonPagedPoolUsage );
        printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n",
                  pmc.QuotaNonPagedPoolUsage );
        printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
        printf( "\tPeakPagefileUsage: 0x%08X\n",
                  pmc.PeakPagefileUsage );
    }

    CloseHandle( hProcess );
}


0
 

Author Comment

by:DebMohanty
ID: 8208062
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 ?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8208131
Are you Debdulal Mohanty from IBM, Bangalore??
0
 

Author Comment

by:DebMohanty
ID: 8208179
No.
0
 
LVL 1

Expert Comment

by:fsign21
ID: 8209401
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.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8209883
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
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9558821
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question