Sign in to follow this  

Obtaining used memory

This topic is 4335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to obtain the memory a program uses. Similar to GlobalMemoryStatus() but for a single program/process. It's C++ on a windows platform. Extra: Is there a way to obtain the CPU usage a single program/process uses?

Share this post


Link to post
Share on other sites
Programmatically or just in general? How about just opening the process manager (ctrl-shift-escape) and switching to Process view?

All the counters used in Process view are available through the Windows Management and Instrumentation API, btw, so you can read this yourself if you want.

Share this post


Link to post
Share on other sites
You are probably interested in the WMI class:

Win32_PerfFormattedData_PerfProc_Process

You will probably want to read the values

* VirtualBytes and VirtualBytesPeak (=total memory in use)
* WorkingSet and WorkingSetPeak (=how much you actively use)
* PercentPrivilegedTime, PercentProcessorTime, and PercentUserTime

Share this post


Link to post
Share on other sites
Do you have a working example? I tried implementing from the MSDN example but even CoInitializeSecurity() doesn't exist despite including all headers in the example + all required headers in the CoInitializeSecurity MSDN reference...
(objbase.h, comdef.h, Wbemidl.h)

Share this post


Link to post
Share on other sites
Quote:
Original post by Magos
Do you have a working example? I tried implementing from the MSDN example but even CoInitializeSecurity() doesn't exist despite including all headers in the example + all required headers in the CoInitializeSecurity MSDN reference...
(objbase.h, comdef.h, Wbemidl.h)

There is versioning within the API. It won't include declarations for versions beyond what you say you need.

The specific function I mentioned requires Windows XP. In order to have the XP stuff included, you need to define some values before #include <windows.h>
#define _WIN32_WINNT 0x0501  /* XP.  500 is Win2K, 502 is Win2K3Server, 600 is Vista*/
#define WINVER 0x0501 /* Matches above */
...Other definitions needed such as #define _WIN32_IE 0x0600 or service-pack elements
#include <windows.h>


Without the versions, it defaults to the oldest version your SDK wants to support, probably Win98.

Share this post


Link to post
Share on other sites
I have it working now (had to define _WIN32_DCOM and link some stuff for things to work).

Anyway, I'm asking this query:
"SELECT * FROM Win32_PerfFormattedData_PerfProc_Process"

And I get about 45 objects back. Why do I get more than one? Do I have to sum them all? I checked the value of VirtualBytes. For one object it's lower than what the task manager says (~x/10). Summing all objects gives way more than what task manager says (~100x).

I'm not sure what I'm getting from the query?

Also, what kind of odd invention is the BSTR string...?

Share this post


Link to post
Share on other sites
Also, after calling IWbemClassObject->Get() to store the result inside a VARIANT, I should use VARIANT.ullVal to get the value, right (as it should be a 64-bit unsigned integer). Doing this I get about 2.2 million as the CPU percentage which doesn't seem too right...

Anyway, here's my code:

Initialization:

HRESULT Result;

if(FAILED(Result = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
Error.SetMessage("Unable to initialize COM!", Result);
return FALSE;
}

COMInitialized = TRUE;

if(FAILED(Result = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL)))
{
Error.SetMessage("Unable to initialize COM security!", Result);
return FALSE;
}

if(FAILED(Result = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, reinterpret_cast<LPVOID*>(&Locator))))
{
Error.SetMessage("Unable to create a COM instance!", Result);
return FALSE;
}

if(FAILED(Result = Locator->ConnectServer(bstr_t("ROOT\\CIMV2"), NULL, NULL, NULL, 0, NULL, NULL, &Services)))
{
Error.SetMessage("Unable to connect to COM server!", Result);
return FALSE;
}

if(FAILED(Result = CoSetProxyBlanket(Services, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE)))
{
Error.SetMessage("Unable to set the COM proxy blanket!", Result);
return FALSE;
}




Cleaning up:

SAFE_RELEASE(Services);
SAFE_RELEASE(Locator);

if(COMInitialized)
{
CoUninitialize();
COMInitialized = FALSE;
}




Asking the query:

ULONG Result;
VARIANT Variant;
IWbemClassObject* Object;
IEnumWbemClassObject* Enumerator;

FreeMemory = 0;
UsedMemory = 0;
CpuUsage = 0;

if(FAILED(Services->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &Enumerator))) return;

VariantInit(&Variant);

while(Enumerator)
{
if(FAILED(Enumerator->Next(WBEM_INFINITE, 1, &Object, &Result))) break;
if(Result == 0) break;

if(SUCCEEDED(Object->Get(L"WorkingSet", 0, &Variant, NULL, NULL)))
{
UsedMemory = static_cast<INT>(Variant.ullVal);
}

if(SUCCEEDED(Object->Get(L"PercentProcessorTime", 0, &Variant, NULL, NULL)))
{
CpuUsage = static_cast<INT>(Variant.ullVal);
}

SAFE_RELEASE(Object);
}

VariantClear(&Variant);

SAFE_RELEASE(Object);
SAFE_RELEASE(Enumerator);


Share this post


Link to post
Share on other sites
Are you looking for something like GetPerformanceInfo? It seems like it'd be far easier to use than something COM-Based, but it requires 2000 or newer.
Hrmm, my local copy of the MSDN docs says 2000 or newer, but the online version says it requires XP.

You can get other information using the PSAPI Functions and the general Processes and Threads Functions, such as using GetProcessTimes to retrieve the start and end time of a process and the amount of cpu time it's used in both kernel and user mode.

Share this post


Link to post
Share on other sites
GetPerformanceInfo() doesn't give info for the process, only system-wide info.

The closest thing GetProcessMemoryInfo() has is the working set which isn't always the amount of memory the program uses).

GetProcessTimes() seems to work as expected though!

Share this post


Link to post
Share on other sites
Err, somehow I mispasted or something - I meant GetProcessMemoryInfo

If that isn't what you want, I'm not sure I know what you do want - it gives all the memory statistics relevant to a process.

GlobalMemoryStatus returns things like total available memory, which are meaningless in the context of a process because a process can use the entire available system memory (including pagefile) up to a hardware-imposed limit (ie 4GB on a 32-bit machine).

Share this post


Link to post
Share on other sites

This topic is 4335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this