Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new operator (as well as malloc) fails to allocate ~ 450 MB of memory [duplicate]

I'm working on a program which stores an array of roughly 220 mln short values in memory. This block of data is allocated like this:

short * arrayName = new short[SIZE_OF_ARRAY];

And then the contents of a file are read into memory. After a big massive update of the overall architecture of the program by another person in the team, this exact line started to crash the program. The message is this:

Microsoft Visual C++ Runtime Library
Runtime Error!
abnormal program termination

It happens immediately at this call of memory allocation (no further lines, such as a check to see if the pointer is NULL, are executed). Even after a few days, it's unclear to us what change in the other code exactly caused this line to start behaving this way (actually nothing which is even remotely linked to this array was changed).

On Linux (Ubuntu, to be exact), everything is working fine; this problem exists only on Windows machines. On 64-bit Windows OSes, this workaround helps (in .pro file):

QMAKE_LFLAGS_WINDOWS += /LARGEADDRESSAWARE

On 32-bit, it doesn't help.

Replacing the line with malloc the following way has let me check if the pointer is NULL after it (which it is) and to get the error code out of errno, which is 12 (ENOMEM) = "Not enough memory".

short * arrayName = (short *)malloc(SIZE_OF_ARRAY * sizeof(short));

This StackOverflow question seems to be about the same problem; its similarity is even up to the point that allocating a smaller amount of memory works (but 450 MB does not). The answers there suggested high memory fragmentation and that new / malloc cannot allocate a continuous memory region, but in my case, the problem persists even freshly after a reboot when only ~ 600 MB out of 2 physical GBs (and 4 virtual GBs) have been used, so it is somewhat ruled out (additionally, like I mentioned, the exact same line of code had worked before).

My main suspicion is that it has something to do with heap size (although I'm not sure if new and malloc both allocate memory to heap; and also I haven't yet found a way to change heap size in Qt). Am I missing something here?

like image 980
Fy Zn Avatar asked Oct 20 '25 13:10

Fy Zn


1 Answers

Memory allocations fail due to lack of address space, not lack of RAM. Lack of RAM causes slow programs, and lots of disk thrashing, as programs get paged out to disk.

/LARGEADDRESSAWARE tells the OS that your app can accept a larger address space. Win64 will provide Win32 apps with 3GB of address space, on Win32 this isn't standard.

Turning off ASLR can help, as it will load DLLs in a linear fashion (thus at predictable offsets, which is a security risk). With ASLR, DLL's are scattered in memory. With only 10 DLLs's scattered through 2 GB of address space (and that is unrealistically low), the average space between them is ~200 MB.

like image 182
MSalters Avatar answered Oct 23 '25 02:10

MSalters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!