Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Getting information about memory

Posted on 2003-03-25
10
Medium Priority
?
205 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
  • 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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

564 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