Jump to content

Allocating memory


SpiderPig
 Share

Go to solution Solved by SpiderPig,

Recommended Posts

In my game I store six 2049 x 2049 height-maps in a dynamically created array, like this;

size_t mapSize = 2049 * 2049;
float* map = new float[mapSize];//approx 16mb

When the app closes they are destroyed like this;

delete[] map;

As far as I can tell, the more data I load in this way, the app will eventually crash with a "bad_alloc" or "memory exception".

It doesn't crash when it allocates for the new map I'm loading, it crashes later on when I'm allocating storage for something else.  It could be another set of 16mb or the set of 50mb being allocated.

Everything will be fine until I add another map.  I've tried various things to rule out corrupted files and what not.  I've also run the windows "memory diagnostic" just to verify that the ram is working.  It should be, I've never had memory issues even with some high RAM demanding games.

This is my error catching code too, the memory used is only about 700mb in total.  RAM usage is nearly 5gb in the task manager out of 32gb system total.

//Class header
float* w1_map;
float* w2_map;
float* w3_map;
float* w4_map;
float* w5_map;
float* w6_map;

//class cpp
void cFragment::InitMaps() {
		size_t _mapSize = 2049 * 2049;
		try {
			w1_map = new float[mapSize];
			w2_map = new float[mapSize];
			w3_map = new float[mapSize];
			w4_map = new float[mapSize];
			w5_map = new float[mapSize];
			w6_map = new float[mapSize];
		}

		catch (std::bad_alloc) {
			long _usedMemory = System::GetMemoryUsage();
			std::stringstream _stream;
			_stream << "Used Memory : " << _usedMemory << "\nFunction : cFragment::InitMaps()\n\nClick OK to Terminate Program.";
			MessageBox(Window::GetCurrent()->GetHandle(), _stream.str().c_str(), "BAD ALLOC", MB_OK);
			exit(NULL);
		}
}

 

So I guess my questions are these;

Has anyone else had similar issues?

Are there limits to how much can be allocated to one pointer?

Are there limits to the amount of consecutive calls to "new[]"?

I have a 64bit system, but as I understand it L4 and visual studio are 32bit, so the most RAM that can be allocated for the entire application is 4gb?

 

Link to comment
Share on other sites

  • Solution

Right-o.  I found the issue.  In Visual Studio an application by default is only allowed to access 2GB of memory.  I guess this to insure that the released application wont use all of the 4GB of ram on a 32bit system so that the operating system will still run?

Anyway, in VS2017 you can enable large Addresses in Property Pages->Linker->System.  This fixed the issue but I think it's only increased to around 3GB, I'm not sure.  Will keep reading and maybe look into exporting it as a 64bit app.  Not sure how this will work with Leadwerks 4 yet...

Some links below;

https://msdn.microsoft.com/en-us/library/wz223b1z.aspx

https://stackoverflow.com/questions/5686459/what-is-the-maximum-memory-available-to-a-c-application-on-32-bit-windows

 

Hopefully someone finds this info useful. ?

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...