aronsatie
aronsatie

Reputation: 237

Developing 32-bit C++ application in Windows 7 64-bit

I develop a Win32 application in Visual Studio 2008 (C++). It runs fine in either 32-bit or 64-bit Windows 7. However, sometimes I need to allocate quite big memory buffers (the application deals with lots of data), and if I do it in Windows 7 64-bit, it fails, in 32-bit it runs fine. By big memory buffers I mean one ~250MB and another ~150MB. I have 8GB RAM installed in my PC, and according to my information, the 64-bit OS makes 4GB availabla for a 32-bit application. I need nowhere near that limit, still malloc fails. Any ideas why and what can I do about it? Thanks in advance.

Upvotes: 2

Views: 1673

Answers (3)

KennyT
KennyT

Reputation: 1

I think there is a bug in malloc under win7-64. I have run the same tests with my 32-bit app on three machines: XP32, w7-32 and win7-64. It runs OK on the 32bit platforms but fails to allocate a 110Mb block under w7-64. I have de-fragmented my drive and tried again from a clean reboot with the same result.

K

Upvotes: -1

arx
arx

Reputation: 16896

150MB and 250MB are not especially huge allocations. As others have noted, the problem you are hitting is most likely address space fragmentation (i.e. there is enough free space, it's just not all in one piece).

In addition to the other suggestions:

  • Allocate the memory as soon as the program starts. The address space will be less likely to be fragmented. If it wouldn't be sensible to allocate this much memory for the life of the program, use VirtualAlloc with the MEM_RESERVE flag to reserve the address space and commit it later.
  • If address space is fragmented as soon as the process starts, it's probably caused by DLLs loading at unhelpful locations. You can use VMMap to see what's happening in the address space. If DLLs belonging to you are fragmenting the address space you can rebase them.

Upvotes: 1

Roman Ryltsov
Roman Ryltsov

Reputation: 69632

You are hitting virtual address space limit in your Win32 binary. The limit might be 2 to 4 GB depending on OS and environment. Actualy limit is less due to allocation fragmentation.

Your choices are to:

Upvotes: 1

Related Questions