malloc() and system memory
Hello avid programmers,
If a system has X MB (say 512 MB) of RAM, how can one determine the maximum space malloc() can allocate without returning a "segmentation fault"? are there any ways I can boundlessly increase this possible allocation size(given I have unlimited swap space?) (in linux? does it matter what system I am using?) Thanks for your responses, -Spaceman |
On a 32-bit system, the logical limit is 2^32-1 bytes (about 4GB)
|
thanks for your response itsme86,
so if my swap space is 4GB or greater, i will never get a Segmentation fault when calling a malloc() in my program if the allocation is less than 4GB?... I am debugging a code I am writing, thats why I would like to know, Thanks, -Spaceman |
AFAIK malloc() never segfaults.
When there's not enough memory, malloc() returns NULL. If you really do get segfaults when calling malloc(), there must be something else you're doing wrong. |
hi itsme86
how can you allocate 4 GB from malloc ?? in linux whole 4 GB is splitted in to 3GB user space and 1 GB kernel space then how can you allocate from kernel space? please let me know if i am wrong Regards Mehul |
Quote:
-spaceman. |
so If I have a swap space of 10GB, I can never get that much space from malloc()??
-spaceman |
Well, malloc's argument is generally a 4 byte integer, so you're definitely not getting 10 gigs out of it : ) The size of your virtual memory is (probably) only 4 gigs, and as mentioned some of that is reserved for kernel memory. Then some more is taken by your app's code and any shared libraries you're using.
Here's a little trick to figure out what values malloc will barf on: Code:
#include <stdio.h> However, memory management on linux is optimistic, so it's possible that you can get a non-null value from malloc and then have the OS start shooting processes when you actually go to use the memory you got. That's called the OOM killer. Realistically, if you need gigs of memory to do whatever computation you're doing, you should figure out a way to write parts you aren't using to disk. |
thanks aluser, thats a great little bit.
-spaceman |
All times are GMT -5. The time now is 01:36 PM. |