Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-03-2002, 06:03 PM   #1
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 354

Rep: Reputation: 78
Out Of Memory - A caring malloc?

One major problem with all the OOM handling ideas are they are hostile. They assume the situation is out of control and desperate.

What the programmers need is help to avoid getting the user into such dire straits. Consider two scenarios....

* Nibbled to Death by Ducks. The program is malloc'ing many tiny chunks of memory. Eventually it starts thrashing. A quarter of an hour later it runs out of memory and malloc returns 0 or some friendly OOM killer hits it on the head. Assume the OOM killer doesn't, what can the programmer do? Pop up a friendly dialog box, and shutdown neatly? Nah! Not enough memory to do that!

* Grabbing a large chunk. I was using Ghost View. For various reasons it asked for a huge amount of memory. Malloc didn't return zero, there was enough swap. However, the system turned to sticky mud and stayed that way until I could kill the X server 15 minutes later...

Now assume that these two programs are written by responsible, caring programmers. What could they have done to stop entering this domain? Nothing. The OS hates users.

Now if the OS had a "memory getting low, system getting slow" signal, and could send that signal to all programs. Then in the Nibble to Death by Ducks scenario the programmer could start bailing out in a clean and friendly manner.

If the OS had a "malloc, but not at the cost of the system" malloc, then any programmer knows when he is going to be grabbing a really large chunk. So when he grabs a large chunk he uses the "friendly caring malloc" and checks the return code. If the answer is bad, he pops up a friendly message that tells the user that he really doesn't want to do that and why. The user may at his own choice and peril say "do so anyway".

I'm sure the friendly caring malloc could be written in userland, any hints?

The "memory getting low, system getting slow" signal needs some OS support I think.

Any suggestions on how to go about this?
Old 09-04-2002, 01:17 AM   #2
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
I don't know about friendly malloc. But I know before dynamic allocations were not possible people used to grab a large chunk of memory and do a cursor based implimentation of lists which allowed them to handle their memory safely and efficently. Its a pity that we still have such hopeless garbage collection mechanisams even today. Any way, if you use node space implimentation you need to have a good approximate idea of how much memory you need. And it is by all means grossly inefficient. But this will atleast will let you know initially itself that if you have enough memory before you do something that causes the whole system to hang.
Any better ideas anyone.? I am also interested in knowing more.
Old 09-08-2002, 05:23 PM   #3
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 354

Original Poster
Rep: Reputation: 78
There is some discussion of developing an API for my idea on the linux-mm mailling list.
Old 09-18-2002, 09:38 AM   #4
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
I've done things similar to what of which sarin is speaking.. but implementing them with malloc as well.. it involves malloc'ing a chunk of memory, enough to handle most stuff, and then using an array of pointers into the malloc'd chunk.. cleanup is necessary to keep the "swap" from fragementing..

My implementation was very simple, but I suppose that it would be possible to also implement a swapfile style access to this block of memory, so that if more memory space was needed, it could save out anything that it could to make room for other stuff. but you're talking about a fairly complex interface.

It's definitely doable, though.


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
CRT Monitors caring tips! Mega Man X General 11 06-28-2005 08:50 AM
malloc eagle683 Programming 6 05-22-2005 02:40 PM
malloc() and system memory spaceman27 Programming 8 04-25-2005 09:58 AM
Sharing is caring KezzerDrix Linux - Newbie 4 03-15-2005 09:33 AM
about malloc eshwar_ind Programming 11 02-18-2004 03:41 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:35 AM.

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