Release vs Debug out of memory

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
rpgplayerrobin
Gnoll
Posts: 617
Joined: Wed Mar 18, 2009 3:03 am
x 353

Release vs Debug out of memory

Post by rpgplayerrobin »

Hey!

When using debug (x64) in Visual Studio 2017 I have been experiencing my game sometimes act strangely.
Looking closer, it seems to have to do with memory, which I can detect if I place this in my render loop (which triggers if I load a scene containing my objects that forces the game to load many textures):

Code: Select all

// Check for not enough memory
#ifdef _DEBUG
try
{
	char* tmpTest = new char[1024 * 1024 * 5]; // 5 MB
	delete[] tmpTest;
}
catch(...)
{
	CGeneric::ShowMessage("CApplication::frameStarted, could not allocate 5 MB, we are out of RAM!");
}
#endif
I have been searching online a long time now and not found a single answer to why this happens in debug but not in release (both in x64).
Therefore I tried another test directly in the main function:

Code: Select all

std::vector<char*> tmpMemoryAllocatedList;

long long tmpTotalMemoryAllocated = 0;
while (true)
{
	try
	{
		const long long tmpMemoryToAllocate = 1024 * 1024 * 1; // 1 MB

		char* tmpTest = new char[tmpMemoryToAllocate];
		tmpMemoryAllocatedList.push_back(tmpTest);

		tmpTotalMemoryAllocated += tmpMemoryToAllocate;
	}
	catch (...)
	{
		break;
	}
}

for (size_t i = 0; i < tmpMemoryAllocatedList.size(); i++)
{
	char* tmpTest = tmpMemoryAllocatedList[i];
	delete[] tmpTest;
}
tmpMemoryAllocatedList.clear();

CGeneric::ShowMessage("Memory allocated before out of memory (MB): " + CGeneric::ToString(tmpTotalMemoryAllocated / 1024 / 1024));

return 0;
The results are pretty shocking.
In debug it is at most 1600 MB before it just won't let me allocate more memory, but in release it allocates all the way up to 26711 MB (though that makes no sense, as I only have 16 GB RAM... :? Does that mean it wrote to my harddrive?).
That is almost 17 times more memory allocated in release compared to debug.

Does anyone know why this is?
How can I make debug be able to allocate more memory than just 1600 MB?
It is pretty bad for my game when debugging, I always have to avoid certain things in the game so the allocation error never happens. :lol:
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: Release vs Debug out of memory

Post by paroj »

maybe this?
When you request a memory block, the debug heap manager allocates from the base heap a slightly larger block of memory than requested and returns a pointer to your portion of that block.
https://docs.microsoft.com/en-us/visual ... ew=vs-2022
rpgplayerrobin
Gnoll
Posts: 617
Joined: Wed Mar 18, 2009 3:03 am
x 353

Re: Release vs Debug out of memory

Post by rpgplayerrobin »

I created a new project to replicate it and that project did not have this issue.

When going through all project settings compared to my own project, it seems I had set "No (/LARGEADDRESSAWARE:NO)" (Project Properties->Linker->System->Enable Large Addresses).

With that set as blank (which then defaults to Yes), I can now have the same amount of memory on debug as release.

Now my game can in debug actually open all maps in my game without crashing and/or black UI! :D
Post Reply