logic: fixed application crash on windows if JVM tries to allocate too much memory by reducing the amount of allocated memory to the available size.

This commit is contained in:
mlangkabel
2018-08-28 13:06:36 +02:00
parent 5f80d79540
commit 21d58357f2
4 changed files with 78 additions and 1 deletions
+46
View File
@@ -0,0 +1,46 @@
#include "utility/utilityWindows.h"
#ifdef WIN32
#include <Windows.h>
unsigned long utility::getLargestByteSizeOfAllocatableMemory()
{
MEMORY_BASIC_INFORMATION mbi;
unsigned long start = 0;
bool recording = false;
unsigned long freestart = 0, largestFreestart = 0;
__int64 free = 0, largestFree = 0;
while (true)
{
SIZE_T s = VirtualQuery((LPCVOID)start, &mbi, sizeof(mbi));
if (s != sizeof(mbi)) break;
if (mbi.State == MEM_FREE)
{
if (!recording) freestart = start;
free += mbi.RegionSize;
recording = true;
}
else
{
if (recording)
{
if (free > largestFree)
{
largestFree = free;
largestFreestart = freestart;
}
}
free = 0;
recording = false;
}
start += mbi.RegionSize;
}
return largestFree;
}
#endif // WIN32