I am using the difference between MEMORYSTATUSEX.ullTotalVirtual and MEMORYSTATUSEX.ullAvailVirtual to display the virtual memory used on the system.
If this number (the difference between the two members) constantly goes up with time as my application executes, is this indicative of a memory leak?
I am somewhat skeptical that it does but would like some input on this.
Multiple calls to ::SystemTimeToFileTime seems to be causing this number to increase over time.
UPDATE:
bool f ( const SYSTEMTIME& sysTime, UINT64* localTime )
{
FILETIME f;
if ( !SystemTimeToFileTime ( sysTime, &f ) )
return ( false );
*localTime = ( static_cast < UINT64 > ( f.dwHighDateTime ) << 32 ) | f.dwLowDateTime;
return ( true );
}
When I execute this function many times, it causes MEMORYSTATUSEX.AvailVirtual to decrease. I don't notice any performance issues in the process which leads me to believe that this is not a memory leak. But I don't understand why this would cause the available virtual memory to decrease. If I comment out this function, then ullAvailVirtual is unaffected.
I am running Windows 7 32-bit. However, my colleague has seen the same behavior on Windows 7 64-bit.
The issue is somewhere else in your program. The following program (including a bug fix to your f
function) shows the same value over and over again, no matter how long you leave it running.
#include <windows.h>
#include <stdio.h>
bool f ( const SYSTEMTIME& sysTime, UINT64* localTime )
{
FILETIME f;
if ( !SystemTimeToFileTime ( &sysTime, &f ) )
return ( false );
*localTime = ( static_cast < UINT64 > ( f.dwHighDateTime ) << 32 ) | f.dwLowDateTime;
return ( true );
}
int __cdecl main(int, char**)
{
SYSTEMTIME st;
GetSystemTime(&st);
for (;;) {
UINT64 result;
for (int i = 0; i < 10000000; i++) f(st, &result);
MEMORYSTATUSEX info = { sizeof(info) };
GlobalMemoryStatusEx(&info);
printf("%I64d\n", info.ullTotalVirtual - info.ullAvailVirtual);
}
}
Note also that the value printed is not the virtual memory being used by the system. It is the amount of address space being used by your process. Specifically, you are subtracting these values
The size of the user-mode portion of the virtual address space of the calling process, in bytes.
The amount of unreserved and uncommitted memory currently in the user-mode portion of the virtual address space of the calling process, in bytes.
It's talking about virtual address space, not virtual memory.