Solved

GlobalMemoryStatus question

Posted on 2009-05-05
4
929 Views
Last Modified: 2013-12-21
Can I use GlobalMemoryStatus to find out how much memory my program is using.

For example
Launch My program
Available memory = GlobalMemoryStatus
Do something in my program
Available memory = GlobalMemoryStatus
Do something in my program
Available memory = GlobalMemoryStatus
etc....

I could then try and find any creeping memory leaks this way.

Is the above method a good way to do this?

0
Comment
Question by:Wanting2LearnMan
4 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 167 total points
Comment Utility
Hi Wanting2LearnMan,

'GlobalMemoryStatus' can be used to gather information about system-wide memory information.

To find information about your program's memory usage you can use 'GetProcessMemoryInfo' from PSAPI, i.e.:

> #include <psapi.h>
> #pragma comment( lib, "psapi.lib" )
> ...
> HANDLE hProcess = GetCurrentProcess();
> PROCESS_MEMORY_COUNTERS info = { sizeof( info ) };
> GetProcessMemoryInfo( hProcess, &info, sizeof( info ) );
>
> int *x = new int [ 200000 ];
>
> GetProcessMemoryInfo( hProcess, &info, sizeof( info ) );
>
> delete [] x;
>
> GetProcessMemoryInfo( hProcess, &info, sizeof( info ) );

Hope that helps,

ZOPPO
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 166 total points
Comment Utility
>>I could then try and find any creeping memory leaks this way.

If your goal is to find memory leaks, that is a way, but there are better ones. E.g.

#ifdef _DEBUG
#ifndef _DBG_NEW

#include <crtdbg.h>

inline void* __operator_new(size_t __n) {
     return ::operator new(__n,_NORMAL_BLOCK,__FILE__,__LINE__);
}
inline void* _cdecl operator new(size_t __n,const char* __fname,int __line) {
     return ::operator new(__n,_NORMAL_BLOCK,__fname,__line);
}
inline void _cdecl operator delete(void* __p,const char*,int) {
     ::operator delete(__p);
}

#define _DBG_NEW new(__FILE__,__LINE__)
#define new _DBG_NEW


#endif // _DBG_NEW
#else

#define __operator_new(__n) operator new(__n)

#endif

Then, add

               int tmpFlag;

               // Get the current state of the flag
               // and store it in a temporary variable
               tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

               // Turn On (OR) - Keep freed memory blocks in the
               // heap's linked list and mark them as freed
               tmpFlag |= _CRTDBG_LEAK_CHECK_DF;

               // Set the new state for the flag
               _CrtSetDbgFlag( tmpFlag );


to your app's start code and call

               _CrtDumpMemoryLeaks ();


to before the program ends and you'll get the line number where the allocation occured.
0
 

Author Comment

by:Wanting2LearnMan
Comment Utility
WHen I do:

 #include <psapi.h>
 #pragma comment( lib, "psapi.lib" )

i get: fatal error LNK1104: cannot open file 'psapi.lib'

How do I solve this problem?  My IDE is eVC++4.0.

Thanks
0
 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 167 total points
Comment Utility
You can use the function you mentioned. For example:
inline void LogMemoryStatus()
{
      SYSTEMTIME stime = { 0 };
      ::GetSystemTime(&stime);
      Trace(TEXT("-----------------"));
      Trace(TEXT("Time: %02d %02d %02d %03d\n"), stime.wHour,
            stime.wMinute, stime.wSecond, stime.wMilliseconds);
      MEMORYSTATUS st = { sizeof(MEMORYSTATUS) };
      ::GlobalMemoryStatus(&st);
      Trace(TEXT("Total: Physical %d, Virtual %d.\n"),
            st.dwTotalPhys, st.dwTotalVirtual);
      Trace(TEXT("Load: %d, Free: Physical %d, Virtual %d\n"),
            st.dwMemoryLoad, st.dwAvailPhys, st.dwAvailVirtual);
      Trace(TEXT("================"));
}
You will have to implement your Trace function - an output to a file/com/whatever.
You can use the remote tools from the SDK installed on you computer together with eVC4 - remote HeapWalker and/or others.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 105
no14 challenge 14 56
FizzBuzz challenge 9 71
C++ Class Serialization, Encapsulation and Formatted Output 8 43
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…
I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now