Original address:
http://blog.kalmbachnet.de/?postid=9
Some Applications Require A Hue Amount of Continues Memory. And Sometimes, Windows Is Not Able To Satisfy The Request.
To Find Out The Reason of Why The Virtual Memory Is Badly Splited and To Get The Size of The Larges Continous Memory Block, You Can Use The Following Code To Get The Info About The Virtual Address Space:
#include
#include
#include
#include
#pragma comment (lib, "psapi.lib")
Void PrintMeminfo (Memory_basic_Information & MBI)
{
IF (Mbi.State! = MEM_FREE)
Return;
_tprintf (_T ("BaseAddress: 0x% 8.8X, 0x% 8.8X, 0x% 8.8X / N"),
Mbi.Baseaddress, Mbi.allocationBase, Mbi.allocationProtect);
_tprintf (_T ("Regionsize: 0x% 8.8x / n"), MBI.REGIONSize;
_tprintf (_T ("state:"));
Switch (Mbi.State)
{
Case Mem_Commit:
_tprintf (_T ("commit / n"));
Break;
Case Mem_Free:
_tprintf (_T ("free / n"));
Break;
Case Mem_Reserve:
_tprintf (_t ("reserve / n"));
Break;
DEFAULT:
_tprintf (_t ("unknown / n"));
Break;
}
_tprintf (_T ("type:"));
Switch (Mbi.Type)
{
Case Mem_Image:
_tprintf (_t ("image / n"));
Break;
Case MEM_MAPPED:
_tprintf (_t ("mapped / n")));
Break;
Case Mem_Private:
_tprintf (_t ("private / n"));
Break;
DEFAULT:
_tprintf (_T ("unknown (% d) / n"), mbi.type);
Break;
}
_tprintf (_t ("/ n"));
}
Void PrintModules (DWORD Processid)
{
HModule HMODS [1024];
Handle HPROCESS;
DWORD CBNEDED;
Unsigned Int i;
_tprintf (_t ("/ nprocess id:% u / n"), processid;
HProcess = OpenProcess (Process_Query_Information | Process_VM_Read, False, Processid; IF (Null HProcess)
Return;
IF (HProcessModules (HProcess, HMODS, SIZEOF (HMODS), & CBNeeded)
{
For (i = 0; cbeneeded / sizeof (hmodule)); i )
{
Tchar SzmodName [MAX_PATH];
IF (GetModuleFileNameex (HProcess, HMODS [I], SzModName, Sizeof (SzmodName)))
{
_tprintf (_t ("/ t% s (0x% 08x) / n"), SzmodName, HMODS [I]);
}
}
}
CloseHandle (HPROCESS);
}
SIZE_T GetLargestFreeMemRegion (LPVOID * LPBASEADDR, BOOL ShowMemInfo)
{
System_info systeminfo;
GetSystemInfo (& SystemInfo);
Void * p = 0;
Memory_basic_information mbi;
SIZE_T LARGESTSIZE = 0;
While (P { SIZE_T DWRET = VirtualQuery (P, & MBI, SIZEOF (MBI)); IF (dwret> 0) { IF (ShowMeminfo) PrintMemInfo (MBI); IF (Mbi.State Mem_Free) { Largestsize { Largestsize = mbi.regionsize; IF (LPBaseAddr! = NULL) * lpbaseaddr = mbi.baseaddress; } } P = (void *) ((char *) p) mbi.regionSize; } Else { IF (ShowMeminfo) _tprintf (_t ("### VirtualQuery Failed (% P) / N"), P); P = (void *) ((char *) p) systemInfo.dwpagesize; } } Return Largestsize; } Int_tmain () { LPVOID BaseAddr; SIZE_T LS = GetLargestFreeMemregion (& BaseAddr, true); PRINTMODULES (GetCurrentProcessid ()); _tprintf ("/ nlargest free region: 0x% 8.8x Bytes AT 0x% 8.8x / n"), LS, BaseAddr; Return 0; }