LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   malloc() and system memory (http://www.linuxquestions.org/questions/programming-9/malloc-and-system-memory-315872/)

spaceman27 04-21-2005 08:19 PM

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

itsme86 04-21-2005 08:38 PM

On a 32-bit system, the logical limit is 2^32-1 bytes (about 4GB)

spaceman27 04-21-2005 11:12 PM

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

Hko 04-22-2005 04:28 AM

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.

mehuljv 04-22-2005 05:14 AM

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

spaceman27 04-22-2005 12:24 PM

Quote:

Originally posted by Hko
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.

Well, i feel it could be because my program is very rudimentary and i don't have any error handling for when malloc returns NULL.

-spaceman.

spaceman27 04-22-2005 12:25 PM

so If I have a swap space of 10GB, I can never get that much space from malloc()??


-spaceman

aluser 04-22-2005 01:04 PM

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>
#include <stdlib.h>
#include <limits.h>
#include <inttypes.h>

int main()
{
        size_t min = 1;
        size_t max = ULONG_MAX;

        while (1) {
                void *p;
                size_t middle;
                if (min >= max)
                        break;
                middle = (size_t) ((((uint64_t)min) + ((uint64_t)max)) / 2LL);
                p = malloc(middle);
                free(p);
                if (p == NULL)
                        max = middle - 1;
                else
                        min = middle + 1;
        }
        printf("%u = %x\n", min, min);
        return 0;
}

On my system, I can get around 2,500,000,000 bytes.

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.

spaceman27 04-25-2005 09:58 AM

thanks aluser, thats a great little bit.
-spaceman


All times are GMT -5. The time now is 03:06 AM.