LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 01-20-2010, 07:35 PM   #1
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Question C, Malloc, and Memory: Just Curious


I don't do as much C programming any more (Mostly C++, Java, a little Perl and PHP) but it seems like I remember being told repeatedly to always check the return value of malloc, so that you know whether or not the system actually was able to allocate the memory you requested.

But I was sitting in a lecture the other day and the speaker stated that because Linux does "lazy allocation," malloc might actually return a non-null pointer even if the memory doesn't actually exist, so you might not know the memory is not available until later in the program.

My first thought was, "How would you know later on if the memory was not available?" Would you just have to wait for the program to crash, or do you check some flag or signal? I talked to the lecturer and he wasn't sure.

So I'm wondering: Is this guy right even right about the lazy allocation issue? And if not, then what is the answer to my question? (There is virtual memory as well, I know, but eventually you have to run out of swap space...)
 
Old 01-20-2010, 07:41 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by CoderMan View Post
I don't do as much C programming any more (Mostly C++, Java, a little Perl and PHP) but it seems like I remember being told repeatedly to always check the return value of malloc, so that you know whether or not the system actually was able to allocate the memory you requested.

But I was sitting in a lecture the other day and the speaker stated that because Linux does "lazy allocation," malloc might actually return a non-null pointer even if the memory doesn't actually exist, so you might not know the memory is not available until later in the program.

My first thought was, "How would you know later on if the memory was not available?" Would you just have to wait for the program to crash, or do you check some flag or signal? I talked to the lecturer and he wasn't sure.

So I'm wondering: Is this guy right even right about the lazy allocation issue? And if not, then what is the answer to my question? (There is virtual memory as well, I know, but eventually you have to run out of swap space...)
The answer is in

man 3 malloc
.
 
Old 01-20-2010, 07:57 PM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by CoderMan View Post
I talked to the lecturer and he wasn't sure.
The lecturer was blowing smoke. malloc() returns NULL if and only if the memory allocation failed.
 
Old 01-20-2010, 08:32 PM   #4
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375

Original Poster
Blog Entries: 24

Rep: Reputation: 43
Quote:
Originally Posted by Sergei Steshenko View Post
The answer is in

man 3 malloc
.
Well, I guess it proves what he is saying. Here is a quote from malloc(3):

Code:
       By  default, Linux follows an optimistic memory allocation strategy.  This means that when malloc() returns non-NULL there is no
       guarantee that the memory really is available.  This is a really bad bug.  In case it turns out that the system is out  of  mem‐
       ory,  one  or  more processes will be killed by the infamous OOM killer.  In case Linux is employed under circumstances where it
       would be less desirable to suddenly lose some randomly picked processes, and moreover the kernel version is sufficiently recent,
       one can switch off this overcommitting behavior using a command like:

           # echo 2 > /proc/sys/vm/overcommit_memory
I'm not sure that entirely answers my question, since most people probably don't have overcommit_memory set to 2. (It seems to be set to 0 on my system.) So you can't really be sure that your malloc will not over-commit in most places that it is deployed.
 
Old 01-20-2010, 09:17 PM   #5
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375

Original Poster
Blog Entries: 24

Rep: Reputation: 43
So, I looked up OOM Killer and got this page:

http://linux-mm.org/OOM_Killer

If I read this correctly, then in "optimistic mode" OOM is supposed to keep processes from running out of memory by killing off the "undeserving" processes until there is enough memory left over for everybody else. But it doesn't describe what happens if some process tries to take even more memory than is needed just to run the kernel (or basic system utilities, for the matter).
 
  


Reply



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
get a 'n' byte aligned memory from malloc kapsikum Programming 1 05-30-2008 01:51 PM
malloc() memory corruption (C++) Coxynelle Programming 1 01-17-2007 04:07 PM
glibc detected *** malloc(): memory corruption: abefroman Linux - Software 2 04-12-2006 12:12 PM
malloc() and system memory spaceman27 Programming 8 04-25-2005 09:58 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 06:19 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