ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...)
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...)
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.