Just out of curiosity....
On a Windows 7 PC (64-bit) with 4Gb RAM my compiler (digitalmars) returns a maximum for malloc of 1.5Gb free memory. Dev-C++ returns a maximum of 2Gb.
What's the trick to get more ?
Malloc
Moderators: hgm, Dann Corbit, Harvey Williamson
-
mar
- Posts: 2552
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: Malloc
That's weird. Are you compiling a 64-bit application?
malloc accepts size_t which should be 64-bit for x64 builds.
EDIT: if you are compiling 32-bit code, windows has a per process limit of 2G virtual address space. Either way you can only address up to 4G in 32-bit mode anyway.
malloc accepts size_t which should be 64-bit for x64 builds.
EDIT: if you are compiling 32-bit code, windows has a per process limit of 2G virtual address space. Either way you can only address up to 4G in 32-bit mode anyway.
-
wgarvin
- Posts: 838
- Joined: Thu Jul 05, 2007 5:03 pm
- Location: British Columbia, Canada
Re: Malloc
Try linking your 32-bit app with the /LARGEADDRESSAWARE flag. That sets a flag in the PE header promising to Windows that you won't crash if it gives you more than 2 GB.mar wrote:That's weird. Are you compiling a 64-bit application?
malloc accepts size_t which should be 64-bit for x64 builds.
EDIT: if you are compiling 32-bit code, windows has a per process limit of 2G virtual address space. Either way you can only address up to 4G in 32-bit mode anyway.
With that flag set, I think your 32-bit process will have almost the full 4 GB of virtual space to play with if you run it on a 64-bit version of Windows.
If you run it on a 32-bit version of Windows (XP or newer), it will still only have 2 GB, unless you booted Windows in the hacky /3GB mode (generally not recommended).
-
mar
- Posts: 2552
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: Malloc
Thanks for the tips, but I would prefer to build a 64-bit application in that particular casewgarvin wrote: Try linking your 32-bit app with the /LARGEADDRESSAWARE flag. That sets a flag in the PE header promising to Windows that you won't crash if it gives you more than 2 GB.
With that flag set, I think your 32-bit process will have almost the full 4 GB of virtual space to play with if you run it on a 64-bit version of Windows.
If you run it on a 32-bit version of Windows (XP or newer), it will still only have 2 GB, unless you booted Windows in the hacky /3GB mode (generally not recommended).
OT: Since I know you are a game programmer, I'd like to use the opportunity to ask about how consoles handle this. PS3/360 are 32-bit, right? Are PC versions 64-bit by default now?
A very large project can hit the 2G limit easily, not speaking about fragmentation when one would allocate very large blocks dynamically.
-
Rebel
- Posts: 6946
- Joined: Thu Aug 18, 2011 12:04 pm
Re: Malloc
Either a 32 or 64 bit compile limits to 2Gb.mar wrote:That's weird. Are you compiling a 64-bit application?
Some example code:
Code: Select all
#define CHT_ENTRIES 165000000 // 165.xxx.xxx (max Dev-C++ compiler)
struct hash
{ long key1;
long key2;
long key3;
} cht[CHT_ENTRIES];
Via malloc:
Code: Select all
struct hash
{ long key1;
long key2;
long key3; };
struct hash *cht;
unsigned int ms;
char *p_memory;
for (ms = 0x7f000000; ms >= 2000; ms -= 100000) // seek till 12 Gb
{ p_memory = malloc(ms); if (p_memory) break; }
printf("Free memory = %d Mb\n\n",ms>>20);
cht=(struct hash *)p_memory; // connect to structure
-
mar
- Posts: 2552
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: Malloc
Strange Ed,Rebel wrote:Either a 32 or 64 bit compile limits to 2Gb.
I just tried to malloc 4 gigs (64-bit build, 8G physical ram) and works just fine.
Are you 100% sure thay your compiler generated 64-bit executable?
You should see 32-bit apps in task manager marked with *32.
Or even better, you can try something like
Code: Select all
assert( sizeof(void *) >= 8 );
-
wgarvin
- Posts: 838
- Joined: Thu Jul 05, 2007 5:03 pm
- Location: British Columbia, Canada
Re: Malloc
Yes, the PS3 and 360 are both 32-bit machines. The 4 GB address space is plenty for them, since they both only have 512 MB of RAM and paging to disk is either not supported or at least, something console games never want to do. In practice, if you need to allocate memory and there isn't any RAM left (or your heap is too fragmented, for example) then you just crash. Console games can often run with very low amounts of free memory remaining, sometimes as little as a couple of megabytes, and still not crash. Sometimes the game will have some "last resort" functions that it invokes when free memory gets really low, that try to scavenge some memory by freeing high-res textures, or something like that. But you can't afford to have memory leaks in a console game because that will always be fatal. I remember a particularly bad one near the end of development on one game I worked on, that was caused by a compiler bug that only showed up with high optimization settings and whole-program optimizations enabled. It caused a piece of correct code to be miscompiled in a way that still worked, but constantly leaked small amounts of memory. Within a couple of hours, a few tens of megabytes had been leaked and that was enough that levels which previously fit reliably in memory would no longer fit, and the game would crash. But only on the final retail build -- other builds used for testing, did not get mis-compiled, and could run for 24 hours with no problems.mar wrote: OT: Since I know you are a game programmer, I'd like to use the opportunity to ask about how consoles handle this. PS3/360 are 32-bit, right? Are PC versions 64-bit by default now?
A very large project can hit the 2G limit easily, not speaking about fragmentation when one would allocate very large blocks dynamically.
I don't play PC games much myself, and I haven't paid much attention to what other developers are shipping (32-bit or 64-bit, etc). For this generation I preferred to just buy and play console games. But for AAA games I imagine 64-bit is preferred now, because many gamers already have more than 4 GB of memory. Players with 4 GB or less are also likely to be running ancient Windows XP or something, like my desktop machine at home
-
Rebel
- Posts: 6946
- Joined: Thu Aug 18, 2011 12:04 pm
Re: Malloc
It's 64 bit, no *32. Perhaps I need to plug-in an extra 4Gb before the compiler grants me more than 2Gb, it's a Windows PC after allmar wrote:Strange Ed,Rebel wrote:Either a 32 or 64 bit compile limits to 2Gb.
I just tried to malloc 4 gigs (64-bit build, 8G physical ram) and works just fine.
Are you 100% sure thay your compiler generated 64-bit executable?
You should see 32-bit apps in task manager marked with *32.
Or even better, you can try something like
to make sure you run a in 64-bit mode.Code: Select all
assert( sizeof(void *) >= 8 );
-
Joost Buijs
- Posts: 1562
- Joined: Thu Jul 16, 2009 10:47 am
- Location: Almere, The Netherlands
Re: Malloc
According to the website of Digital Mars their latest C++ compiler does not support x64 builds under Windows yet.Rebel wrote:Just out of curiosity....
On a Windows 7 PC (64-bit) with 4Gb RAM my compiler (digitalmars) returns a maximum for malloc of 1.5Gb free memory. Dev-C++ returns a maximum of 2Gb.
What's the trick to get more ?
I think that the MinGW compiler that comes default with Dev C++ does not support x64 builds either, although it is possible to use a different version of GCC under Dev C++.
Probably this is why you are bound to a 2gB. limit.
-
mar
- Posts: 2552
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: Malloc
Well the system should be able to give you more than you have physical memory (if your swap file is large enough),Rebel wrote:It's 64 bit, no *32. Perhaps I need to plug-in an extra 4Gb before the compiler grants me more than 2Gb, it's a Windows PC after all
so the call should actually succeed and even be fast because it won't start
swapping until you access the memory and the system starts geting page faults.