tags:

views:

121

answers:

4

Hi, How much data can be malloced and how is the limit determined? I am writing an algorithm in C that basically utilizes repeatedly some data stored in arrays. My idea is to have this saved in dynamically allocated arrays but I am not sure if it's possible to have such amounts malloced.

I use 200 arrays of size 2046 holding complex data of size 8 byte each. I use these throughout the process so I do not wish to calculate it over and over.

What are your thoughts about feasibility of such an approach?

Thanks Mir

+3  A: 

A 32bit OS has a limit of 4Gb, typically some (upto half on win32) are reserved for the operating system - mapping the address space of graphcis card memory etc.

Linux supports 64Gb of address space (using Intel's 36bit PAE) on 32bit versions.
EDIT: although each process is limited to 4Gb
The main problem with allocating large amounts of memory is if you need it to be locked in RAM - then you obviously need a lot of RAM. Or if you need it all to be contiguous - it's much easier to get 4 * 1Gb chunks of memory than a single 4Gb chunk with nothing else in the way.

A common approach is to allocate all the memory you need at the start of the program so you can be sure that if the app isn't going to be possible it will fail instantly rather than when it's done 90% of the work.
Don't run other memory intensive apps at the same time.
There are also a bunch of flags you can use to suggest to the kernel that this app should get priority in memory or keep memory locked in ram - sorry it's too long since i did HPC on linux and i'm probably out of date with modern kernels.

Martin Beckett
PAE allows the system to use up to 64GB of physical memory. It does not allow processes to break the 4 GB barrier because virtual addresses are still 32-bit.
siride
ok. Preallocation seems like a good idea. Will try that out. Thanks
Meeir Aalie
@Meeir Aalie - thanks, edit answer to reflect this
Martin Beckett
+3  A: 

How much memory malloc() can allocate depends on:

  • How much memory your program can address directly
  • How much physical memory is available
  • How much swap space is available

On a modern, flat-memory-model 32-bit system, your program can address 4 gigabytes, but some of the address space (usually 2 gigabytes, sometimes 1 gigabyte) is reserved for the kernel. So, as a rule of thumb, you should be able to allocate almost two gigabytes at once, assuming you have the physical memory and swap space to back it up.

On a 64-bit system, running a 64-bit OS and a 64-bit program, your addressable memory is essentially unlimited.

200 arrays of 2048 bytes each is only 400k, which should fit in cache (even on a smartphone).

Commodore Jaeger
Page alignment could balloon it to anywhere from 800kiB to 12800kiB though.
Ignacio Vazquez-Abrams
The OMAP3 has at most 256k L2 cache. 16k data, 16k instruction L1 cache. And the OMAP3 is pretty fast and big as smart phones go.
Amigable Clark Kant
Don't forget that libraries and your program take up a chunk of that 3 GB available, as does the range below the start of your program that is always off limits to catch pointer bugs. Granted, all of that only adds up to a few tens of megabytes usually (for a console app), so the only real problem is that it fragments the address space, making large contiguous allocations impossible.
siride
You missed virtual memory fragmentation. If the block you need has a shared library stuck into the middle of it then you cannot allocate a single block: you need to malloc two blocks and manage it somehow.
Zan Lynx
Sometimes I forget that the shiny new smartphone chips I work on at my day job aren't actually shipping yet.
Commodore Jaeger
+1  A: 

I think that on most mordern (64bit) systems you can allocate 4GB at a time with a malloc( size_t ) call if that much memory is available. How big is each of those 'complex data' entries? if they are of the size 256 bytes, then you'll only need to allocate 100MB.

256bytes × 200 arrays × 2048 entries = 104857600bytes  
104857600 bytes / 1024 / 1024 = 100MB.

So for 4096bytes each that's still only 1600MB or ≃ 1.6GB so it is feasible on most systems today, my four year old laptop got 3GB internal memory. Sometimes I does image manipulation with GIMP and it takes up over 2GB of memory.

Frank
My datatype is only 8 byte long so this fits perfectly. Thanks
Meeir Aalie
Unless you are on 64-bit, you absolutely never ever can allocate a 4GB chunk of virtual memory.
siride
@siride: You're right, I missed a few words.
Frank
A: 

With some implementations of malloc(), the regions are not actually backed by memory until they really get used so you can in theory carry on forever (though in practice of course the list of allocated regions assigned to your process in the kernel takes up space, so you might find you can only call malloc() a few million times even if it never actually gives you any memory). It's called "optimistic allocation" and is the strategy used by Linux (which is why it then has the OOM killer, for when it was over-optimistic).

Graham Lee