9 #include "fwMemory/tools/Win32MemoryMonitorTools.hpp" 11 #include <fwCore/base.hpp> 28 Win32MemoryMonitorTools::Win32MemoryMonitorTools()
34 Win32MemoryMonitorTools::~Win32MemoryMonitorTools()
40 std::uint64_t Win32MemoryMonitorTools::estimateFreeMem()
42 std::uint64_t freeMemory = 0;
44 freeMemory = getFreeSystemMemory();
46 std::uint64_t windowsLimitForAProcess = 1.2 * 1024 * 1024 * 1024;
47 std::uint64_t freeMem = std::min(
48 getFreeSystemMemory(), windowsLimitForAProcess - getUsedProcessMemory());
49 freeMemory = std::max((std::uint64_t) 0, freeMem );
56 void Win32MemoryMonitorTools::printProcessMemoryInformation()
58 DWORD processID = GetCurrentProcessId();
61 hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ,
65 if ( NULL != hProcess )
69 char* name =
new char[nameSize];
74 SLM_INFO(
"-- Process memory information --" );
75 if ( EnumProcessModules( hProcess, &hMod,
sizeof(hMod), &cbNeeded) )
77 GetModuleBaseNameW( hProcess, hMod, (LPWSTR)name, nameSize );
78 OSLM_INFO(
" Name : " << name <<
" ( PID : " << processID <<
" )");
82 OSLM_INFO(
" Name : not found ( PID : " << processID <<
" )");
85 PROCESS_MEMORY_COUNTERS_EX pmc;
86 if ( GetProcessMemoryInfo( hProcess, reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc),
sizeof(pmc)) )
88 OSLM_INFO(
" PageFaultCount : " << (
int)pmc.PageFaultCount );
89 OSLM_INFO(
" PeakWorkingSetSize : " << (
int)pmc.PeakWorkingSetSize );
90 OSLM_INFO(
" WorkingSetSize : " << (
int)pmc.WorkingSetSize );
91 OSLM_INFO(
" QuotaPeakPagedPoolUsage : " << (
int)pmc.QuotaPeakPagedPoolUsage );
92 OSLM_INFO(
" QuotaPagedPoolUsage : " << (
int)pmc.QuotaPagedPoolUsage );
93 OSLM_INFO(
" QuotaPeakNonPagedPoolUsage : " << (
int)pmc.QuotaPeakNonPagedPoolUsage );
94 OSLM_INFO(
" QuotaNonPagedPoolUsage : " << (
int)pmc.QuotaNonPagedPoolUsage );
95 OSLM_INFO(
" PagefileUsage : " << (
int)pmc.PagefileUsage );
96 OSLM_INFO(
" PeakPagefileUsage : " << (
int)pmc.PeakPagefileUsage );
97 OSLM_INFO(
" PrivateUsage : " << (
int)pmc.PrivateUsage );
101 SLM_WARN(
"GetProcessMemoryInfo failed !");
105 CloseHandle( hProcess );
111 void Win32MemoryMonitorTools::printSystemMemoryInformation()
113 std::uint64_t oToKo = 1024;
115 MEMORYSTATUSEX statex;
117 statex.dwLength =
sizeof (statex);
118 GlobalMemoryStatusEx(&statex);
120 SLM_INFO(
"-- System memory information --" );
121 OSLM_INFO(
" There is " << statex.dwMemoryLoad <<
" percent of memory in use." );
122 OSLM_INFO(
" There are " << statex.ullTotalPhys/oToKo <<
" total Ko of physical memory." );
123 OSLM_INFO(
" There are " << statex.ullAvailPhys/oToKo <<
" free Ko of physical memory." );
124 OSLM_INFO(
" There are " << statex.ullTotalPageFile/oToKo <<
" total Ko of paging file." );
125 OSLM_INFO(
" There are " << statex.ullAvailPageFile/oToKo <<
" free Ko of paging file." );
126 OSLM_INFO(
" There are " << statex.ullTotalVirtual/oToKo <<
" total Ko of virtual memory." );
127 OSLM_INFO(
" There are " << statex.ullAvailVirtual/oToKo <<
" free Ko of virtual memory." );
132 void Win32MemoryMonitorTools::printMemoryInformation()
134 printSystemMemoryInformation();
135 printProcessMemoryInformation();
140 std::uint64_t Win32MemoryMonitorTools::getTotalSystemMemory()
142 MEMORYSTATUSEX statex;
144 statex.dwLength =
sizeof (statex);
145 GlobalMemoryStatusEx(&statex);
147 return statex.ullTotalPhys;
152 std::uint64_t Win32MemoryMonitorTools::getUsedSystemMemory()
154 MEMORYSTATUSEX statex;
156 statex.dwLength =
sizeof (statex);
157 GlobalMemoryStatusEx(&statex);
159 return statex.ullTotalPhys - statex.ullAvailPhys;
164 std::uint64_t Win32MemoryMonitorTools::getFreeSystemMemory()
166 MEMORYSTATUSEX statex;
168 statex.dwLength =
sizeof (statex);
169 GlobalMemoryStatusEx(&statex);
171 return statex.ullAvailPhys;
176 std::uint64_t Win32MemoryMonitorTools::getUsedProcessMemory()
181 PROCESS_MEMORY_COUNTERS_EX pmc;
183 GetProcessMemoryInfo( GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc),
sizeof(pmc)) )
185 memory = pmc.WorkingSetSize;
187 SLM_WARN_IF(
"GetProcessMemoryInfo failed !", !result);
The namespace fwMemory contains tools to manage memory. Use for dump.
#define OSLM_INFO(message)
#define SLM_WARN(message)
The namespace memory contains tools to manage memory. It is used for dump. It allows to define the bu...
#define SLM_INFO(message)
#define SLM_WARN_IF(message, cond)