UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » Handling malloc failure

#1 2006-05-17 04:51 AM

Lloyd
Member
Registered: 2006-02-13
Posts: 53

Re: Handling malloc failure

In a program, if the malloc call fails how it can be handled? without a newly allocated memory my program could not run. So what to do to get the memory allocated? Thanks in advance.
Lloyd

Offline

#2 2006-05-17 09:01 AM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: Handling malloc failure


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#3 2006-05-17 09:25 AM

Lloyd
Member
Registered: 2006-02-13
Posts: 53

Re: Handling malloc failure

Sorry, my program is not crashing and is working fine. But I didn't write a code to handle the malloc failure (By assuming chance of malloc failure is very minimal).  I would like to handle malloc failure as well, I can I do that? If my program is in a situation as I mentioned below.

With out the newly allocated memory the program cannot run
But I want to continue the execution of the program.. How can I manage this?
By simply performing some wait?

Offline

#4 2006-05-17 10:34 AM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: Handling malloc failure

arg i don't really know, maybe you could check for allocated memory that you could free?


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#5 2006-05-17 01:52 PM

Lloyd
Member
Registered: 2006-02-13
Posts: 53

Re: Handling malloc failure

But In my program I am freeing memory a soon as possible, so there is no chance of thinking of freeing some memory. Any other way?

Offline

#6 2006-05-17 01:55 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Handling malloc failure

You need to allocate memory in order to run, so if malloc() fails to allocate it, I think
the only sane thing to do is NOT RUN...  Treat it as a fatal error condition...  Unless
this is some hyper-important, can never ever die, must always stay running no matter
what the conditions, sort of app...  In that case, your app will just have to find a way
to make due without the requested memory...  (Maybe try using stack space instead,
if it's really critical to have this memory now...  Or, maybe set aside a block of static
memory at the start of the app, which you can fall-back on for use in emergency
situations like this...)  Or, maybe hunt down other apps on the system to kill, in order
to free up more memory...

Of course, one thing to be aware of is that some systems (Linux being one major
example) are designed around overcommitting of memory, such that malloc()
basically will never fail, even if you request more than is freely available...  Instead,
if the system can't give you enough memory when you REALLY want it (ie: when you
access that allocated memory), you'll get hit with a seg-fault or similar...  (At least
on Linux, I believe this behavior is now configurable: you can turn it off, or you can
choose from a few different overcommit schemes...)

Offline

#7 2006-05-17 02:04 PM

Lloyd
Member
Registered: 2006-02-13
Posts: 53

Re: Handling malloc failure

Thanks Rob.

Offline

#8 2006-05-17 06:50 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Handling malloc failure

To clarify and prevent misconceptions: Applications shouldn't rely on overcommit or the OOM killer to solve the memory failure problem for them. If your application asks for more memory than the system has then the malloc will fail, no matter what. There's no excuse for sloppy error handling, only for quitting the program when a critical malloc fails.

There are basically two situations when a memory allocation fails:

1) Either other programs use more memory than they should, bothering your program with their greed. This is easy to work around, just sleep a while and retry the malloc, especially if you tried allocating just a little bit memory. You can be quite sure than other programs which are more fragile will just exit when their malloc fails, freeing up some memory for you to use. Survival of the stubborn. ;-)

2) Your application is the one using too much memory. This one is hard to solve. Best would be just use less, but that's easier said than done. The problem of memory is that there's either enough, or not. that makes graceful degration tough. Best would be if you could decrease the load on your program somehow and thus back out of the corner you worked yourself in. But in a well designed program you probably already use no more memory than really needed. It depends on what kind of program you have how you can solve this.

Of course reality is often a combination of both. Exiting because a malloc fails at startup is the prefered way, because the one starting it can immediately see it and do something about it. But if you think your program is important and allocation fails later on, then sleeping and retrying seems like a good idea to me. Don't try forever, but be subborn enough that others have time to kill themselves. If your program is doing some work which can be saved to disk then do that before exiting. Keep in mind that most things will probably fails when you try to do them with little free memory left, but a write (+ mmap if needed) call should be save enough.

Offline

#9 2006-05-17 07:41 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Handling malloc failure

Offline

#10 2006-05-17 09:53 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Handling malloc failure

true, true.

Memory is certainly not equal to ram, it's merely an abstract thing applications take for granted. ;-)

Offline

  • Index
  • » C++
  • » Handling malloc failure

Board footer

Powered by FluxBB