Memory usage
-
On what operating system? If you want to keep track of total memory usage, you'll need a way to determine the size in memory of all code (e.g. application and dynamically linked libraries) and stack, which will vary depending on OS. For dynamic heap memory usage, you can make your own implementation of new/delete that counts allocated heap memory - this is the approach taken by memory leak detectors. Have a look at VLD which is an open-source memory leak detector for Visual C++.
-
See here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
CString csMsg;
PROCESS_MEMORY_COUNTERS_EX procMemInfo = {0};
procMemInfo.cb = sizeof(procMemInfo);
if (GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&procMemInfo, sizeof(procMemInfo)))
{
// Log how much physical and total memory we are using
// - Win7 (and newer) reports commit-size in this member
if (procMemInfo.PagefileUsage==0)
procMemInfo.PagefileUsage = procMemInfo.PrivateUsage;
ULONG ulWorkingSetSize = (ULONG)(procMemInfo.WorkingSetSize / 1024 / 1024);
ULONG ulPagefileUsage = (ULONG)(procMemInfo.PagefileUsage / 1024 / 1024);
CString csProcessMemInfo;
csProcessMemInfo.Format(_T("WorkingSetSize=%lu MBytes, CommitChargeSize=%lu MBytes"), ulWorkingSetSize, ulPagefileUsage);
csMsg += csProcessMemInfo;
}
MEMORYSTATUSEX memStatus = {0};
memStatus.dwLength = sizeof(memStatus);
if (GlobalMemoryStatusEx(&memStatus))
{
// Log how much address space we are using (detect memory fragmentation)
ULONG ulUsedVirtual = (ULONG)((memStatus.ullTotalVirtual-memStatus.ullAvailVirtual) / 1024 / 1024);
ULONG ulAvailVirtual = (ULONG)(memStatus.ullAvailVirtual / 1024 / 1024);
CString csMemStatus;
csMemStatus.Format(_T("UsedVirtual=%lu MBytes, AvailableVirtual=%lu MBytes"), ulUsedVirtual, ulAvailVirtual);
csMsg += csMemStatus;
}