LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-21-2005, 08:19 PM   #1
spaceman27
LQ Newbie
 
Registered: Oct 2004
Location: Los Angeles
Distribution: Fedora Core 2
Posts: 15

Rep: Reputation: 0
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
 
Old 04-21-2005, 08:38 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
On a 32-bit system, the logical limit is 2^32-1 bytes (about 4GB)
 
Old 04-21-2005, 11:12 PM   #3
spaceman27
LQ Newbie
 
Registered: Oct 2004
Location: Los Angeles
Distribution: Fedora Core 2
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 04-22-2005, 04:28 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 04-22-2005, 05:14 AM   #5
mehuljv
Member
 
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
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
 
Old 04-22-2005, 12:24 PM   #6
spaceman27
LQ Newbie
 
Registered: Oct 2004
Location: Los Angeles
Distribution: Fedora Core 2
Posts: 15

Original Poster
Rep: Reputation: 0
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.
 
Old 04-22-2005, 12:25 PM   #7
spaceman27
LQ Newbie
 
Registered: Oct 2004
Location: Los Angeles
Distribution: Fedora Core 2
Posts: 15

Original Poster
Rep: Reputation: 0
so If I have a swap space of 10GB, I can never get that much space from malloc()??


-spaceman
 
Old 04-22-2005, 01:04 PM   #8
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.
 
Old 04-25-2005, 09:58 AM   #9
spaceman27
LQ Newbie
 
Registered: Oct 2004
Location: Los Angeles
Distribution: Fedora Core 2
Posts: 15

Original Poster
Rep: Reputation: 0
thanks aluser, thats a great little bit.
-spaceman
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
not all memory is detected by the system superandrzej Slackware 10 11-19-2005 08:11 AM
System Using a lot of memory hamish Linux - Software 4 11-14-2004 04:20 AM
Displaying system memory Neorio Linux - General 2 04-01-2004 07:53 PM
system memory juno Linux - General 2 12-19-2002 04:19 AM
Out Of Memory - A caring malloc? cyent Programming 3 09-18-2002 09:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration