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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-20-2010, 07:35 PM
|
#1
|
|
Member
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Rep:
|
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...)
|
|
|
|
01-20-2010, 07:41 PM
|
#2
|
|
Senior Member
Registered: May 2005
Posts: 4,420
|
Quote:
Originally Posted by CoderMan
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
.
|
|
|
|
01-20-2010, 07:57 PM
|
#3
|
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 806
Rep: 
|
Quote:
Originally Posted by CoderMan
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.
|
|
|
|
01-20-2010, 08:32 PM
|
#4
|
|
Member
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Original Poster
Rep:
|
Quote:
Originally Posted by Sergei Steshenko
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.
|
|
|
|
01-20-2010, 09:17 PM
|
#5
|
|
Member
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Original Poster
Rep:
|
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).
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|