7

i m using malloc to allocate memory and memory requirements are greater than 1GB. so program is crashing...

i want to ask whether this problem can be solved??If yes than how??

my RAM size is 3GB and using 32 bit windows os and programming using vc++

1
  • You're not trying to malloc() a single 1GB chunk, are you? Aug 28, 2009 at 9:56

3 Answers 3

13

The per-process memory limit is 2GB (unless you use the Windows /3GB switch). However, you are probably running into memory fragmentation. When the memory gets fragmented (visualize with VMMap), you will not be able to allocate a large contiguous block. Your options are:

  • Allocate in smaller blocks (preferred)
  • Find some way to defragment the memory
  • Upgrade to a 64-bit OS (you can still compile 32-bit code, but you will have up to 4GB for user mode memory - remember to turn on the large memory aware compiler flag; if you compile 64-bit code, it won't run at all on a 32-bit OS, but the memory limit is much higher than 4GB)
  • If you need that much memory for loading a file, you might be able to memory map the file
  • You might be able to allocate the largest blocks earlier than the smaller blocks.
2

On a 32 bit windows OS , each process gets 4 GB virtual address space out of which 2 GB is available for user and 2 GB is for Kernel. So whatever allocations done should be within this 2 GB space. Note that this 2 GB contains other things such as the other dlls which are loaded by your application, so this memory is fragmented. When you do malloc(), since malloc guarantees that it returns a continuos block of memory the CRT will try to find 1 GB of continuos free memory which may not be avaiable, hence the malloc() fails. One way of solving this would be to use the memory mapped files and map only a portion of the required memory into your virtual address space.

2

If there isn't enough continuos space in the virtual memory space it is not possible to real map memory block to it to be used. So you should check your memory areas.

Where do your dll and exes map to? There is a tool rebase.exe which is part of the ms vc framework which will allow you to realloc the position your dll or exe are loaded to. This could give you more free virtual memory space which will allow you to get greater memory blocks.

The profile option of the "dependency walker" will show you where your dlls and exe are loaded to.

vmmap from sysinternals could help to get a better understanding how mwmory is fragmented and handled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.